flash.c 8.4 KB

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