usb-tusb6010.c 8.6 KB

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