flash.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Hammerhead board-specific flash initialization
  3. *
  4. * Copyright (C) 2008 Miromico AG
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/mtd/mtd.h>
  13. #include <linux/mtd/partitions.h>
  14. #include <linux/mtd/physmap.h>
  15. #include <linux/usb/isp116x.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/delay.h>
  19. #include <mach/portmux.h>
  20. #include <mach/at32ap700x.h>
  21. #include <mach/smc.h>
  22. #include "../../mach-at32ap/clock.h"
  23. #include "flash.h"
  24. #define HAMMERHEAD_USB_PERIPH_GCLK0 0x40000000
  25. #define HAMMERHEAD_USB_PERIPH_CS2 0x02000000
  26. #define HAMMERHEAD_USB_PERIPH_EXTINT0 0x02000000
  27. #define HAMMERHEAD_FPGA_PERIPH_MOSI 0x00000002
  28. #define HAMMERHEAD_FPGA_PERIPH_SCK 0x00000020
  29. #define HAMMERHEAD_FPGA_PERIPH_EXTINT3 0x10000000
  30. static struct smc_timing flash_timing __initdata = {
  31. .ncs_read_setup = 0,
  32. .nrd_setup = 40,
  33. .ncs_write_setup = 0,
  34. .nwe_setup = 10,
  35. .ncs_read_pulse = 80,
  36. .nrd_pulse = 40,
  37. .ncs_write_pulse = 65,
  38. .nwe_pulse = 55,
  39. .read_cycle = 120,
  40. .write_cycle = 120,
  41. };
  42. static struct smc_config flash_config __initdata = {
  43. .bus_width = 2,
  44. .nrd_controlled = 1,
  45. .nwe_controlled = 1,
  46. .byte_write = 1,
  47. };
  48. static struct mtd_partition flash_parts[] = {
  49. {
  50. .name = "u-boot",
  51. .offset = 0x00000000,
  52. .size = 0x00020000, /* 128 KiB */
  53. .mask_flags = MTD_WRITEABLE,
  54. },
  55. {
  56. .name = "root",
  57. .offset = 0x00020000,
  58. .size = 0x007d0000,
  59. },
  60. {
  61. .name = "env",
  62. .offset = 0x007f0000,
  63. .size = 0x00010000,
  64. .mask_flags = MTD_WRITEABLE,
  65. },
  66. };
  67. static struct physmap_flash_data flash_data = {
  68. .width = 2,
  69. .nr_parts = ARRAY_SIZE(flash_parts),
  70. .parts = flash_parts,
  71. };
  72. static struct resource flash_resource = {
  73. .start = 0x00000000,
  74. .end = 0x007fffff,
  75. .flags = IORESOURCE_MEM,
  76. };
  77. static struct platform_device flash_device = {
  78. .name = "physmap-flash",
  79. .id = 0,
  80. .resource = &flash_resource,
  81. .num_resources = 1,
  82. .dev = { .platform_data = &flash_data, },
  83. };
  84. #ifdef CONFIG_BOARD_HAMMERHEAD_USB
  85. static struct smc_timing isp1160_timing __initdata = {
  86. .ncs_read_setup = 75,
  87. .nrd_setup = 75,
  88. .ncs_write_setup = 75,
  89. .nwe_setup = 75,
  90. /* We use conservative timing settings, as the minimal settings aren't
  91. stable. There may be room for tweaking. */
  92. .ncs_read_pulse = 75, /* min. 33ns */
  93. .nrd_pulse = 75, /* min. 33ns */
  94. .ncs_write_pulse = 75, /* min. 26ns */
  95. .nwe_pulse = 75, /* min. 26ns */
  96. .read_cycle = 225, /* min. 143ns */
  97. .write_cycle = 225, /* min. 136ns */
  98. };
  99. static struct smc_config isp1160_config __initdata = {
  100. .bus_width = 2,
  101. .nrd_controlled = 1,
  102. .nwe_controlled = 1,
  103. .byte_write = 0,
  104. };
  105. /*
  106. * The platform delay function is only used to enforce the strange
  107. * read to write delay. This can not be configured in the SMC. All other
  108. * timings are controlled by the SMC (see timings obove)
  109. * So in isp116x-hcd.c we should comment out USE_PLATFORM_DELAY
  110. */
  111. void isp116x_delay(struct device *dev, int delay)
  112. {
  113. if (delay > 150)
  114. ndelay(delay - 150);
  115. }
  116. static struct isp116x_platform_data isp1160_data = {
  117. .sel15Kres = 1, /* use internal downstream resistors */
  118. .oc_enable = 0, /* external overcurrent detection */
  119. .int_edge_triggered = 0, /* interrupt is level triggered */
  120. .int_act_high = 0, /* interrupt is active low */
  121. .delay = isp116x_delay, /* platform delay function */
  122. };
  123. static struct resource isp1160_resource[] = {
  124. {
  125. .start = 0x08000000,
  126. .end = 0x08000001,
  127. .flags = IORESOURCE_MEM,
  128. },
  129. {
  130. .start = 0x08000002,
  131. .end = 0x08000003,
  132. .flags = IORESOURCE_MEM,
  133. },
  134. {
  135. .start = 64,
  136. .flags = IORESOURCE_IRQ,
  137. },
  138. };
  139. static struct platform_device isp1160_device = {
  140. .name = "isp116x-hcd",
  141. .id = 0,
  142. .resource = isp1160_resource,
  143. .num_resources = 3,
  144. .dev = {
  145. .platform_data = &isp1160_data,
  146. },
  147. };
  148. #endif
  149. #ifdef CONFIG_BOARD_HAMMERHEAD_USB
  150. static int __init hammerhead_usbh_init(void)
  151. {
  152. struct clk *gclk;
  153. struct clk *osc;
  154. int ret;
  155. /* setup smc for usbh */
  156. smc_set_timing(&isp1160_config, &isp1160_timing);
  157. ret = smc_set_configuration(2, &isp1160_config);
  158. if (ret < 0) {
  159. printk(KERN_ERR
  160. "hammerhead: failed to set ISP1160 USBH timing\n");
  161. return ret;
  162. }
  163. /* setup gclk0 to run from osc1 */
  164. gclk = clk_get(NULL, "gclk0");
  165. if (IS_ERR(gclk))
  166. goto err_gclk;
  167. osc = clk_get(NULL, "osc1");
  168. if (IS_ERR(osc))
  169. goto err_osc;
  170. if (clk_set_parent(gclk, osc)) {
  171. pr_debug("hammerhead: failed to set osc1 for USBH clock\n");
  172. goto err_set_clk;
  173. }
  174. /* set clock to 6MHz */
  175. clk_set_rate(gclk, 6000000);
  176. /* and enable */
  177. clk_enable(gclk);
  178. /* select GCLK0 peripheral function */
  179. at32_select_periph(GPIO_PIOA_BASE, HAMMERHEAD_USB_PERIPH_GCLK0,
  180. GPIO_PERIPH_A, 0);
  181. /* enable CS2 peripheral function */
  182. at32_select_periph(GPIO_PIOE_BASE, HAMMERHEAD_USB_PERIPH_CS2,
  183. GPIO_PERIPH_A, 0);
  184. /* H_WAKEUP must be driven low */
  185. at32_select_gpio(GPIO_PIN_PA(8), AT32_GPIOF_OUTPUT);
  186. /* Select EXTINT0 for PB25 */
  187. at32_select_periph(GPIO_PIOB_BASE, HAMMERHEAD_USB_PERIPH_EXTINT0,
  188. GPIO_PERIPH_A, 0);
  189. /* register usbh device driver */
  190. platform_device_register(&isp1160_device);
  191. err_set_clk:
  192. clk_put(osc);
  193. err_osc:
  194. clk_put(gclk);
  195. err_gclk:
  196. return ret;
  197. }
  198. #endif
  199. #ifdef CONFIG_BOARD_HAMMERHEAD_FPGA
  200. static struct smc_timing fpga_timing __initdata = {
  201. .ncs_read_setup = 16,
  202. .nrd_setup = 32,
  203. .ncs_read_pulse = 48,
  204. .nrd_pulse = 32,
  205. .read_cycle = 64,
  206. .ncs_write_setup = 16,
  207. .nwe_setup = 16,
  208. .ncs_write_pulse = 32,
  209. .nwe_pulse = 32,
  210. .write_cycle = 64,
  211. };
  212. static struct smc_config fpga_config __initdata = {
  213. .bus_width = 4,
  214. .nrd_controlled = 1,
  215. .nwe_controlled = 1,
  216. .byte_write = 0,
  217. };
  218. static struct resource hh_fpga0_resource[] = {
  219. {
  220. .start = 0xffe00400,
  221. .end = 0xffe00400 + 0x3ff,
  222. .flags = IORESOURCE_MEM,
  223. },
  224. {
  225. .start = 4,
  226. .end = 4,
  227. .flags = IORESOURCE_IRQ,
  228. },
  229. {
  230. .start = 0x0c000000,
  231. .end = 0x0c000100,
  232. .flags = IORESOURCE_MEM,
  233. },
  234. {
  235. .start = 67,
  236. .end = 67,
  237. .flags = IORESOURCE_IRQ,
  238. },
  239. };
  240. static u64 hh_fpga0_dma_mask = DMA_32BIT_MASK;
  241. static struct platform_device hh_fpga0_device = {
  242. .name = "hh_fpga",
  243. .id = 0,
  244. .dev = {
  245. .dma_mask = &hh_fpga0_dma_mask,
  246. .coherent_dma_mask = DMA_32BIT_MASK,
  247. },
  248. .resource = hh_fpga0_resource,
  249. .num_resources = ARRAY_SIZE(hh_fpga0_resource),
  250. };
  251. static struct clk hh_fpga0_spi_clk = {
  252. .name = "spi_clk",
  253. .dev = &hh_fpga0_device.dev,
  254. .mode = pba_clk_mode,
  255. .get_rate = pba_clk_get_rate,
  256. .index = 1,
  257. };
  258. struct platform_device *__init at32_add_device_hh_fpga(void)
  259. {
  260. /* Select peripheral functionallity for SPI SCK and MOSI */
  261. at32_select_periph(GPIO_PIOB_BASE, HAMMERHEAD_FPGA_PERIPH_SCK,
  262. GPIO_PERIPH_B, 0);
  263. at32_select_periph(GPIO_PIOB_BASE, HAMMERHEAD_FPGA_PERIPH_MOSI,
  264. GPIO_PERIPH_B, 0);
  265. /* reserve all other needed gpio
  266. * We have on board pull ups, so there is no need
  267. * to enable gpio pull ups */
  268. /* INIT_DONE (input) */
  269. at32_select_gpio(GPIO_PIN_PB(0), 0);
  270. /* nSTATUS (input) */
  271. at32_select_gpio(GPIO_PIN_PB(2), 0);
  272. /* nCONFIG (output, low) */
  273. at32_select_gpio(GPIO_PIN_PB(3), AT32_GPIOF_OUTPUT);
  274. /* CONF_DONE (input) */
  275. at32_select_gpio(GPIO_PIN_PB(4), 0);
  276. /* Select EXTINT3 for PB28 (Interrupt from FPGA) */
  277. at32_select_periph(GPIO_PIOB_BASE, HAMMERHEAD_FPGA_PERIPH_EXTINT3,
  278. GPIO_PERIPH_A, 0);
  279. /* Get our parent clock */
  280. hh_fpga0_spi_clk.parent = clk_get(NULL, "pba");
  281. clk_put(hh_fpga0_spi_clk.parent);
  282. /* Register clock in at32 clock tree */
  283. at32_clk_register(&hh_fpga0_spi_clk);
  284. platform_device_register(&hh_fpga0_device);
  285. return &hh_fpga0_device;
  286. }
  287. #endif
  288. /* This needs to be called after the SMC has been initialized */
  289. static int __init hammerhead_flash_init(void)
  290. {
  291. int ret;
  292. smc_set_timing(&flash_config, &flash_timing);
  293. ret = smc_set_configuration(0, &flash_config);
  294. if (ret < 0) {
  295. printk(KERN_ERR "hammerhead: failed to set NOR flash timing\n");
  296. return ret;
  297. }
  298. platform_device_register(&flash_device);
  299. #ifdef CONFIG_BOARD_HAMMERHEAD_USB
  300. hammerhead_usbh_init();
  301. #endif
  302. #ifdef CONFIG_BOARD_HAMMERHEAD_FPGA
  303. /* Setup SMC for FPGA interface */
  304. smc_set_timing(&fpga_config, &fpga_timing);
  305. ret = smc_set_configuration(3, &fpga_config);
  306. #endif
  307. if (ret < 0) {
  308. printk(KERN_ERR "hammerhead: failed to set FPGA timing\n");
  309. return ret;
  310. }
  311. return 0;
  312. }
  313. device_initcall(hammerhead_flash_init);