platform-flash.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright 2011, Netlogic Microsystems.
  3. * Copyright 2004, Matt Porter <mporter@kernel.crashing.org>
  4. *
  5. * This file is licensed under the terms of the GNU General Public
  6. * License version 2. This program is licensed "as is" without any
  7. * warranty of any kind, whether express or implied.
  8. */
  9. #include <linux/device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/delay.h>
  15. #include <linux/ioport.h>
  16. #include <linux/resource.h>
  17. #include <linux/spi/flash.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/physmap.h>
  20. #include <linux/mtd/rawnand.h>
  21. #include <linux/mtd/partitions.h>
  22. #include <asm/netlogic/haldefs.h>
  23. #include <asm/netlogic/xlr/iomap.h>
  24. #include <asm/netlogic/xlr/flash.h>
  25. #include <asm/netlogic/xlr/bridge.h>
  26. #include <asm/netlogic/xlr/gpio.h>
  27. #include <asm/netlogic/xlr/xlr.h>
  28. /*
  29. * Default NOR partition layout
  30. */
  31. static struct mtd_partition xlr_nor_parts[] = {
  32. {
  33. .name = "User FS",
  34. .offset = 0x800000,
  35. .size = MTDPART_SIZ_FULL,
  36. }
  37. };
  38. /*
  39. * Default NAND partition layout
  40. */
  41. static struct mtd_partition xlr_nand_parts[] = {
  42. {
  43. .name = "Root Filesystem",
  44. .offset = 64 * 64 * 2048,
  45. .size = 432 * 64 * 2048,
  46. },
  47. {
  48. .name = "Home Filesystem",
  49. .offset = MTDPART_OFS_APPEND,
  50. .size = MTDPART_SIZ_FULL,
  51. },
  52. };
  53. /* Use PHYSMAP flash for NOR */
  54. struct physmap_flash_data xlr_nor_data = {
  55. .width = 2,
  56. .parts = xlr_nor_parts,
  57. .nr_parts = ARRAY_SIZE(xlr_nor_parts),
  58. };
  59. static struct resource xlr_nor_res[] = {
  60. {
  61. .flags = IORESOURCE_MEM,
  62. },
  63. };
  64. static struct platform_device xlr_nor_dev = {
  65. .name = "physmap-flash",
  66. .dev = {
  67. .platform_data = &xlr_nor_data,
  68. },
  69. .num_resources = ARRAY_SIZE(xlr_nor_res),
  70. .resource = xlr_nor_res,
  71. };
  72. /*
  73. * Use "gen_nand" driver for NAND flash
  74. *
  75. * There seems to be no way to store a private pointer containing
  76. * platform specific info in gen_nand drivier. We will use a global
  77. * struct for now, since we currently have only one NAND chip per board.
  78. */
  79. struct xlr_nand_flash_priv {
  80. int cs;
  81. uint64_t flash_mmio;
  82. };
  83. static struct xlr_nand_flash_priv nand_priv;
  84. static void xlr_nand_ctrl(struct mtd_info *mtd, int cmd,
  85. unsigned int ctrl)
  86. {
  87. if (ctrl & NAND_CLE)
  88. nlm_write_reg(nand_priv.flash_mmio,
  89. FLASH_NAND_CLE(nand_priv.cs), cmd);
  90. else if (ctrl & NAND_ALE)
  91. nlm_write_reg(nand_priv.flash_mmio,
  92. FLASH_NAND_ALE(nand_priv.cs), cmd);
  93. }
  94. struct platform_nand_data xlr_nand_data = {
  95. .chip = {
  96. .nr_chips = 1,
  97. .nr_partitions = ARRAY_SIZE(xlr_nand_parts),
  98. .chip_delay = 50,
  99. .partitions = xlr_nand_parts,
  100. },
  101. .ctrl = {
  102. .cmd_ctrl = xlr_nand_ctrl,
  103. },
  104. };
  105. static struct resource xlr_nand_res[] = {
  106. {
  107. .flags = IORESOURCE_MEM,
  108. },
  109. };
  110. static struct platform_device xlr_nand_dev = {
  111. .name = "gen_nand",
  112. .id = -1,
  113. .num_resources = ARRAY_SIZE(xlr_nand_res),
  114. .resource = xlr_nand_res,
  115. .dev = {
  116. .platform_data = &xlr_nand_data,
  117. }
  118. };
  119. /*
  120. * XLR/XLS supports upto 8 devices on its FLASH interface. The value in
  121. * FLASH_BAR (on the MEM/IO bridge) gives the base for mapping all the
  122. * flash devices.
  123. * Under this, each flash device has an offset and size given by the
  124. * CSBASE_ADDR and CSBASE_MASK registers for the device.
  125. *
  126. * The CSBASE_ registers are expected to be setup by the bootloader.
  127. */
  128. static void setup_flash_resource(uint64_t flash_mmio,
  129. uint64_t flash_map_base, int cs, struct resource *res)
  130. {
  131. u32 base, mask;
  132. base = nlm_read_reg(flash_mmio, FLASH_CSBASE_ADDR(cs));
  133. mask = nlm_read_reg(flash_mmio, FLASH_CSADDR_MASK(cs));
  134. res->start = flash_map_base + ((unsigned long)base << 16);
  135. res->end = res->start + (mask + 1) * 64 * 1024;
  136. }
  137. static int __init xlr_flash_init(void)
  138. {
  139. uint64_t gpio_mmio, flash_mmio, flash_map_base;
  140. u32 gpio_resetcfg, flash_bar;
  141. int cs, boot_nand, boot_nor;
  142. /* Flash address bits 39:24 is in bridge flash BAR */
  143. flash_bar = nlm_read_reg(nlm_io_base, BRIDGE_FLASH_BAR);
  144. flash_map_base = (flash_bar & 0xffff0000) << 8;
  145. gpio_mmio = nlm_mmio_base(NETLOGIC_IO_GPIO_OFFSET);
  146. flash_mmio = nlm_mmio_base(NETLOGIC_IO_FLASH_OFFSET);
  147. /* Get the chip reset config */
  148. gpio_resetcfg = nlm_read_reg(gpio_mmio, GPIO_PWRON_RESET_CFG_REG);
  149. /* Check for boot flash type */
  150. boot_nor = boot_nand = 0;
  151. if (nlm_chip_is_xls()) {
  152. /* On XLS, check boot from NAND bit (GPIO reset reg bit 16) */
  153. if (gpio_resetcfg & (1 << 16))
  154. boot_nand = 1;
  155. /* check boot from PCMCIA, (GPIO reset reg bit 15 */
  156. if ((gpio_resetcfg & (1 << 15)) == 0)
  157. boot_nor = 1; /* not set, booted from NOR */
  158. } else { /* XLR */
  159. /* check boot from PCMCIA (bit 16 in GPIO reset on XLR) */
  160. if ((gpio_resetcfg & (1 << 16)) == 0)
  161. boot_nor = 1; /* not set, booted from NOR */
  162. }
  163. /* boot flash at chip select 0 */
  164. cs = 0;
  165. if (boot_nand) {
  166. nand_priv.cs = cs;
  167. nand_priv.flash_mmio = flash_mmio;
  168. setup_flash_resource(flash_mmio, flash_map_base, cs,
  169. xlr_nand_res);
  170. /* Initialize NAND flash at CS 0 */
  171. nlm_write_reg(flash_mmio, FLASH_CSDEV_PARM(cs),
  172. FLASH_NAND_CSDEV_PARAM);
  173. nlm_write_reg(flash_mmio, FLASH_CSTIME_PARMA(cs),
  174. FLASH_NAND_CSTIME_PARAMA);
  175. nlm_write_reg(flash_mmio, FLASH_CSTIME_PARMB(cs),
  176. FLASH_NAND_CSTIME_PARAMB);
  177. pr_info("ChipSelect %d: NAND Flash %pR\n", cs, xlr_nand_res);
  178. return platform_device_register(&xlr_nand_dev);
  179. }
  180. if (boot_nor) {
  181. setup_flash_resource(flash_mmio, flash_map_base, cs,
  182. xlr_nor_res);
  183. pr_info("ChipSelect %d: NOR Flash %pR\n", cs, xlr_nor_res);
  184. return platform_device_register(&xlr_nor_dev);
  185. }
  186. return 0;
  187. }
  188. arch_initcall(xlr_flash_init);