usb-tusb6010.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * linux/arch/arm/mach-omap2/usb-tusb6010.c
  3. *
  4. * Copyright (C) 2006 Nokia Corporation
  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/string.h>
  11. #include <linux/types.h>
  12. #include <linux/errno.h>
  13. #include <linux/delay.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/gpio.h>
  16. #include <linux/export.h>
  17. #include <linux/usb/musb.h>
  18. #include <plat/gpmc.h>
  19. #include "mux.h"
  20. static u8 async_cs, sync_cs;
  21. static unsigned refclk_psec;
  22. /* t2_ps, when quantized to fclk units, must happen no earlier than
  23. * the clock after after t1_NS.
  24. *
  25. * Return a possibly updated value of t2_ps, converted to nsec.
  26. */
  27. static unsigned
  28. next_clk(unsigned t1_NS, unsigned t2_ps, unsigned fclk_ps)
  29. {
  30. unsigned t1_ps = t1_NS * 1000;
  31. unsigned t1_f, t2_f;
  32. if ((t1_ps + fclk_ps) < t2_ps)
  33. return t2_ps / 1000;
  34. t1_f = (t1_ps + fclk_ps - 1) / fclk_ps;
  35. t2_f = (t2_ps + fclk_ps - 1) / fclk_ps;
  36. if (t1_f >= t2_f)
  37. t2_f = t1_f + 1;
  38. return (t2_f * fclk_ps) / 1000;
  39. }
  40. /* NOTE: timings are from tusb 6010 datasheet Rev 1.8, 12-Sept 2006 */
  41. static int tusb_set_async_mode(unsigned sysclk_ps, unsigned fclk_ps)
  42. {
  43. struct gpmc_timings t;
  44. unsigned t_acsnh_advnh = sysclk_ps + 3000;
  45. unsigned tmp;
  46. memset(&t, 0, sizeof(t));
  47. /* CS_ON = t_acsnh_acsnl */
  48. t.cs_on = 8;
  49. /* ADV_ON = t_acsnh_advnh - t_advn */
  50. t.adv_on = next_clk(t.cs_on, t_acsnh_advnh - 7000, fclk_ps);
  51. /*
  52. * READ ... from omap2420 TRM fig 12-13
  53. */
  54. /* ADV_RD_OFF = t_acsnh_advnh */
  55. t.adv_rd_off = next_clk(t.adv_on, t_acsnh_advnh, fclk_ps);
  56. /* OE_ON = t_acsnh_advnh + t_advn_oen (then wait for nRDY) */
  57. t.oe_on = next_clk(t.adv_on, t_acsnh_advnh + 1000, fclk_ps);
  58. /* ACCESS = counters continue only after nRDY */
  59. tmp = t.oe_on * 1000 + 300;
  60. t.access = next_clk(t.oe_on, tmp, fclk_ps);
  61. /* OE_OFF = after data gets sampled */
  62. tmp = t.access * 1000;
  63. t.oe_off = next_clk(t.access, tmp, fclk_ps);
  64. t.cs_rd_off = t.oe_off;
  65. tmp = t.cs_rd_off * 1000 + 7000 /* t_acsn_rdy_z */;
  66. t.rd_cycle = next_clk(t.cs_rd_off, tmp, fclk_ps);
  67. /*
  68. * WRITE ... from omap2420 TRM fig 12-15
  69. */
  70. /* ADV_WR_OFF = t_acsnh_advnh */
  71. t.adv_wr_off = t.adv_rd_off;
  72. /* WE_ON = t_acsnh_advnh + t_advn_wen (then wait for nRDY) */
  73. t.we_on = next_clk(t.adv_wr_off, t_acsnh_advnh + 1000, fclk_ps);
  74. /* WE_OFF = after data gets sampled */
  75. tmp = t.we_on * 1000 + 300;
  76. t.we_off = next_clk(t.we_on, tmp, fclk_ps);
  77. t.cs_wr_off = t.we_off;
  78. tmp = t.cs_wr_off * 1000 + 7000 /* t_acsn_rdy_z */;
  79. t.wr_cycle = next_clk(t.cs_wr_off, tmp, fclk_ps);
  80. return gpmc_cs_set_timings(async_cs, &t);
  81. }
  82. static int tusb_set_sync_mode(unsigned sysclk_ps, unsigned fclk_ps)
  83. {
  84. struct gpmc_timings t;
  85. unsigned t_scsnh_advnh = sysclk_ps + 3000;
  86. unsigned tmp;
  87. memset(&t, 0, sizeof(t));
  88. t.cs_on = 8;
  89. /* ADV_ON = t_acsnh_advnh - t_advn */
  90. t.adv_on = next_clk(t.cs_on, t_scsnh_advnh - 7000, fclk_ps);
  91. /* GPMC_CLK rate = fclk rate / div */
  92. t.sync_clk = 11100 /* 11.1 nsec */;
  93. tmp = (t.sync_clk + fclk_ps - 1) / fclk_ps;
  94. if (tmp > 4)
  95. return -ERANGE;
  96. if (tmp <= 0)
  97. tmp = 1;
  98. t.page_burst_access = (fclk_ps * tmp) / 1000;
  99. /*
  100. * READ ... based on omap2420 TRM fig 12-19, 12-20
  101. */
  102. /* ADV_RD_OFF = t_scsnh_advnh */
  103. t.adv_rd_off = next_clk(t.adv_on, t_scsnh_advnh, fclk_ps);
  104. /* OE_ON = t_scsnh_advnh + t_advn_oen * fclk_ps (then wait for nRDY) */
  105. tmp = (t.adv_rd_off * 1000) + (3 * fclk_ps);
  106. t.oe_on = next_clk(t.adv_on, tmp, fclk_ps);
  107. /* ACCESS = number of clock cycles after t_adv_eon */
  108. tmp = (t.oe_on * 1000) + (5 * fclk_ps);
  109. t.access = next_clk(t.oe_on, tmp, fclk_ps);
  110. /* OE_OFF = after data gets sampled */
  111. tmp = (t.access * 1000) + (1 * fclk_ps);
  112. t.oe_off = next_clk(t.access, tmp, fclk_ps);
  113. t.cs_rd_off = t.oe_off;
  114. tmp = t.cs_rd_off * 1000 + 7000 /* t_scsn_rdy_z */;
  115. t.rd_cycle = next_clk(t.cs_rd_off, tmp, fclk_ps);
  116. /*
  117. * WRITE ... based on omap2420 TRM fig 12-21
  118. */
  119. /* ADV_WR_OFF = t_scsnh_advnh */
  120. t.adv_wr_off = t.adv_rd_off;
  121. /* WE_ON = t_scsnh_advnh + t_advn_wen * fclk_ps (then wait for nRDY) */
  122. tmp = (t.adv_wr_off * 1000) + (3 * fclk_ps);
  123. t.we_on = next_clk(t.adv_wr_off, tmp, fclk_ps);
  124. /* WE_OFF = number of clock cycles after t_adv_wen */
  125. tmp = (t.we_on * 1000) + (6 * fclk_ps);
  126. t.we_off = next_clk(t.we_on, tmp, fclk_ps);
  127. t.cs_wr_off = t.we_off;
  128. tmp = t.cs_wr_off * 1000 + 7000 /* t_scsn_rdy_z */;
  129. t.wr_cycle = next_clk(t.cs_wr_off, tmp, fclk_ps);
  130. return gpmc_cs_set_timings(sync_cs, &t);
  131. }
  132. extern unsigned long gpmc_get_fclk_period(void);
  133. /* tusb driver calls this when it changes the chip's clocking */
  134. int tusb6010_platform_retime(unsigned is_refclk)
  135. {
  136. static const char error[] =
  137. KERN_ERR "tusb6010 %s retime error %d\n";
  138. unsigned fclk_ps = gpmc_get_fclk_period();
  139. unsigned sysclk_ps;
  140. int status;
  141. if (!refclk_psec || fclk_ps == 0)
  142. return -ENODEV;
  143. sysclk_ps = is_refclk ? refclk_psec : TUSB6010_OSCCLK_60;
  144. status = tusb_set_async_mode(sysclk_ps, fclk_ps);
  145. if (status < 0) {
  146. printk(error, "async", status);
  147. goto done;
  148. }
  149. status = tusb_set_sync_mode(sysclk_ps, fclk_ps);
  150. if (status < 0)
  151. printk(error, "sync", status);
  152. done:
  153. return status;
  154. }
  155. EXPORT_SYMBOL_GPL(tusb6010_platform_retime);
  156. static struct resource tusb_resources[] = {
  157. /* Order is significant! The start/end fields
  158. * are updated during setup..
  159. */
  160. { /* Asynchronous access */
  161. .flags = IORESOURCE_MEM,
  162. },
  163. { /* Synchronous access */
  164. .flags = IORESOURCE_MEM,
  165. },
  166. { /* IRQ */
  167. .name = "mc",
  168. .flags = IORESOURCE_IRQ,
  169. },
  170. };
  171. static u64 tusb_dmamask = ~(u32)0;
  172. static struct platform_device tusb_device = {
  173. .name = "musb-tusb",
  174. .id = -1,
  175. .dev = {
  176. .dma_mask = &tusb_dmamask,
  177. .coherent_dma_mask = 0xffffffff,
  178. },
  179. .num_resources = ARRAY_SIZE(tusb_resources),
  180. .resource = tusb_resources,
  181. };
  182. /* this may be called only from board-*.c setup code */
  183. int __init
  184. tusb6010_setup_interface(struct musb_hdrc_platform_data *data,
  185. unsigned ps_refclk, unsigned waitpin,
  186. unsigned async, unsigned sync,
  187. unsigned irq, unsigned dmachan)
  188. {
  189. int status;
  190. static char error[] __initdata =
  191. KERN_ERR "tusb6010 init error %d, %d\n";
  192. /* ASYNC region, primarily for PIO */
  193. status = gpmc_cs_request(async, SZ_16M, (unsigned long *)
  194. &tusb_resources[0].start);
  195. if (status < 0) {
  196. printk(error, 1, status);
  197. return status;
  198. }
  199. tusb_resources[0].end = tusb_resources[0].start + 0x9ff;
  200. async_cs = async;
  201. gpmc_cs_write_reg(async, GPMC_CS_CONFIG1,
  202. GPMC_CONFIG1_PAGE_LEN(2)
  203. | GPMC_CONFIG1_WAIT_READ_MON
  204. | GPMC_CONFIG1_WAIT_WRITE_MON
  205. | GPMC_CONFIG1_WAIT_PIN_SEL(waitpin)
  206. | GPMC_CONFIG1_READTYPE_ASYNC
  207. | GPMC_CONFIG1_WRITETYPE_ASYNC
  208. | GPMC_CONFIG1_DEVICESIZE_16
  209. | GPMC_CONFIG1_DEVICETYPE_NOR
  210. | GPMC_CONFIG1_MUXADDDATA);
  211. /* SYNC region, primarily for DMA */
  212. status = gpmc_cs_request(sync, SZ_16M, (unsigned long *)
  213. &tusb_resources[1].start);
  214. if (status < 0) {
  215. printk(error, 2, status);
  216. return status;
  217. }
  218. tusb_resources[1].end = tusb_resources[1].start + 0x9ff;
  219. sync_cs = sync;
  220. gpmc_cs_write_reg(sync, GPMC_CS_CONFIG1,
  221. GPMC_CONFIG1_READMULTIPLE_SUPP
  222. | GPMC_CONFIG1_READTYPE_SYNC
  223. | GPMC_CONFIG1_WRITEMULTIPLE_SUPP
  224. | GPMC_CONFIG1_WRITETYPE_SYNC
  225. | GPMC_CONFIG1_CLKACTIVATIONTIME(1)
  226. | GPMC_CONFIG1_PAGE_LEN(2)
  227. | GPMC_CONFIG1_WAIT_READ_MON
  228. | GPMC_CONFIG1_WAIT_WRITE_MON
  229. | GPMC_CONFIG1_WAIT_PIN_SEL(waitpin)
  230. | GPMC_CONFIG1_DEVICESIZE_16
  231. | GPMC_CONFIG1_DEVICETYPE_NOR
  232. | GPMC_CONFIG1_MUXADDDATA
  233. /* fclk divider gets set later */
  234. );
  235. /* IRQ */
  236. status = gpio_request_one(irq, GPIOF_IN, "TUSB6010 irq");
  237. if (status < 0) {
  238. printk(error, 3, status);
  239. return status;
  240. }
  241. tusb_resources[2].start = irq + IH_GPIO_BASE;
  242. /* set up memory timings ... can speed them up later */
  243. if (!ps_refclk) {
  244. printk(error, 4, status);
  245. return -ENODEV;
  246. }
  247. refclk_psec = ps_refclk;
  248. status = tusb6010_platform_retime(1);
  249. if (status < 0) {
  250. printk(error, 5, status);
  251. return status;
  252. }
  253. /* finish device setup ... */
  254. if (!data) {
  255. printk(error, 6, status);
  256. return -ENODEV;
  257. }
  258. tusb_device.dev.platform_data = data;
  259. /* REVISIT let the driver know what DMA channels work */
  260. if (!dmachan)
  261. tusb_device.dev.dma_mask = NULL;
  262. else {
  263. /* assume OMAP 2420 ES2.0 and later */
  264. if (dmachan & (1 << 0))
  265. omap_mux_init_signal("sys_ndmareq0", 0);
  266. if (dmachan & (1 << 1))
  267. omap_mux_init_signal("sys_ndmareq1", 0);
  268. if (dmachan & (1 << 2))
  269. omap_mux_init_signal("sys_ndmareq2", 0);
  270. if (dmachan & (1 << 3))
  271. omap_mux_init_signal("sys_ndmareq3", 0);
  272. if (dmachan & (1 << 4))
  273. omap_mux_init_signal("sys_ndmareq4", 0);
  274. if (dmachan & (1 << 5))
  275. omap_mux_init_signal("sys_ndmareq5", 0);
  276. }
  277. /* so far so good ... register the device */
  278. status = platform_device_register(&tusb_device);
  279. if (status < 0) {
  280. printk(error, 7, status);
  281. return status;
  282. }
  283. return 0;
  284. }