usb-tusb6010.c 8.7 KB

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