ipu-dmfc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright (c) 2010 Sascha Hauer <s.hauer@pengutronix.de>
  3. * Copyright (C) 2005-2009 Freescale Semiconductor, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <linux/export.h>
  16. #include <linux/types.h>
  17. #include <linux/errno.h>
  18. #include <linux/io.h>
  19. #include <video/imx-ipu-v3.h>
  20. #include "ipu-prv.h"
  21. #define DMFC_RD_CHAN 0x0000
  22. #define DMFC_WR_CHAN 0x0004
  23. #define DMFC_WR_CHAN_DEF 0x0008
  24. #define DMFC_DP_CHAN 0x000c
  25. #define DMFC_DP_CHAN_DEF 0x0010
  26. #define DMFC_GENERAL1 0x0014
  27. #define DMFC_GENERAL2 0x0018
  28. #define DMFC_IC_CTRL 0x001c
  29. #define DMFC_WR_CHAN_ALT 0x0020
  30. #define DMFC_WR_CHAN_DEF_ALT 0x0024
  31. #define DMFC_DP_CHAN_ALT 0x0028
  32. #define DMFC_DP_CHAN_DEF_ALT 0x002c
  33. #define DMFC_GENERAL1_ALT 0x0030
  34. #define DMFC_STAT 0x0034
  35. #define DMFC_WR_CHAN_1_28 0
  36. #define DMFC_WR_CHAN_2_41 8
  37. #define DMFC_WR_CHAN_1C_42 16
  38. #define DMFC_WR_CHAN_2C_43 24
  39. #define DMFC_DP_CHAN_5B_23 0
  40. #define DMFC_DP_CHAN_5F_27 8
  41. #define DMFC_DP_CHAN_6B_24 16
  42. #define DMFC_DP_CHAN_6F_29 24
  43. #define DMFC_FIFO_SIZE_64 (3 << 3)
  44. #define DMFC_FIFO_SIZE_128 (2 << 3)
  45. #define DMFC_FIFO_SIZE_256 (1 << 3)
  46. #define DMFC_FIFO_SIZE_512 (0 << 3)
  47. #define DMFC_SEGMENT(x) ((x & 0x7) << 0)
  48. #define DMFC_BURSTSIZE_128 (0 << 6)
  49. #define DMFC_BURSTSIZE_64 (1 << 6)
  50. #define DMFC_BURSTSIZE_32 (2 << 6)
  51. #define DMFC_BURSTSIZE_16 (3 << 6)
  52. struct dmfc_channel_data {
  53. int ipu_channel;
  54. unsigned long channel_reg;
  55. unsigned long shift;
  56. unsigned eot_shift;
  57. unsigned max_fifo_lines;
  58. };
  59. static const struct dmfc_channel_data dmfcdata[] = {
  60. {
  61. .ipu_channel = IPUV3_CHANNEL_MEM_BG_SYNC,
  62. .channel_reg = DMFC_DP_CHAN,
  63. .shift = DMFC_DP_CHAN_5B_23,
  64. .eot_shift = 20,
  65. .max_fifo_lines = 3,
  66. }, {
  67. .ipu_channel = 24,
  68. .channel_reg = DMFC_DP_CHAN,
  69. .shift = DMFC_DP_CHAN_6B_24,
  70. .eot_shift = 22,
  71. .max_fifo_lines = 1,
  72. }, {
  73. .ipu_channel = IPUV3_CHANNEL_MEM_FG_SYNC,
  74. .channel_reg = DMFC_DP_CHAN,
  75. .shift = DMFC_DP_CHAN_5F_27,
  76. .eot_shift = 21,
  77. .max_fifo_lines = 2,
  78. }, {
  79. .ipu_channel = IPUV3_CHANNEL_MEM_DC_SYNC,
  80. .channel_reg = DMFC_WR_CHAN,
  81. .shift = DMFC_WR_CHAN_1_28,
  82. .eot_shift = 16,
  83. .max_fifo_lines = 2,
  84. }, {
  85. .ipu_channel = 29,
  86. .channel_reg = DMFC_DP_CHAN,
  87. .shift = DMFC_DP_CHAN_6F_29,
  88. .eot_shift = 23,
  89. .max_fifo_lines = 1,
  90. },
  91. };
  92. #define DMFC_NUM_CHANNELS ARRAY_SIZE(dmfcdata)
  93. struct ipu_dmfc_priv;
  94. struct dmfc_channel {
  95. unsigned slots;
  96. unsigned slotmask;
  97. unsigned segment;
  98. int burstsize;
  99. struct ipu_soc *ipu;
  100. struct ipu_dmfc_priv *priv;
  101. const struct dmfc_channel_data *data;
  102. };
  103. struct ipu_dmfc_priv {
  104. struct ipu_soc *ipu;
  105. struct device *dev;
  106. struct dmfc_channel channels[DMFC_NUM_CHANNELS];
  107. struct mutex mutex;
  108. unsigned long bandwidth_per_slot;
  109. void __iomem *base;
  110. int use_count;
  111. };
  112. int ipu_dmfc_enable_channel(struct dmfc_channel *dmfc)
  113. {
  114. struct ipu_dmfc_priv *priv = dmfc->priv;
  115. mutex_lock(&priv->mutex);
  116. if (!priv->use_count)
  117. ipu_module_enable(priv->ipu, IPU_CONF_DMFC_EN);
  118. priv->use_count++;
  119. mutex_unlock(&priv->mutex);
  120. return 0;
  121. }
  122. EXPORT_SYMBOL_GPL(ipu_dmfc_enable_channel);
  123. static void ipu_dmfc_wait_fifos(struct ipu_dmfc_priv *priv)
  124. {
  125. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  126. while ((readl(priv->base + DMFC_STAT) & 0x02fff000) != 0x02fff000) {
  127. if (time_after(jiffies, timeout)) {
  128. dev_warn(priv->dev,
  129. "Timeout waiting for DMFC FIFOs to clear\n");
  130. break;
  131. }
  132. cpu_relax();
  133. }
  134. }
  135. void ipu_dmfc_disable_channel(struct dmfc_channel *dmfc)
  136. {
  137. struct ipu_dmfc_priv *priv = dmfc->priv;
  138. mutex_lock(&priv->mutex);
  139. priv->use_count--;
  140. if (!priv->use_count) {
  141. ipu_dmfc_wait_fifos(priv);
  142. ipu_module_disable(priv->ipu, IPU_CONF_DMFC_EN);
  143. }
  144. if (priv->use_count < 0)
  145. priv->use_count = 0;
  146. mutex_unlock(&priv->mutex);
  147. }
  148. EXPORT_SYMBOL_GPL(ipu_dmfc_disable_channel);
  149. static int ipu_dmfc_setup_channel(struct dmfc_channel *dmfc, int slots,
  150. int segment, int burstsize)
  151. {
  152. struct ipu_dmfc_priv *priv = dmfc->priv;
  153. u32 val, field;
  154. dev_dbg(priv->dev,
  155. "dmfc: using %d slots starting from segment %d for IPU channel %d\n",
  156. slots, segment, dmfc->data->ipu_channel);
  157. switch (slots) {
  158. case 1:
  159. field = DMFC_FIFO_SIZE_64;
  160. break;
  161. case 2:
  162. field = DMFC_FIFO_SIZE_128;
  163. break;
  164. case 4:
  165. field = DMFC_FIFO_SIZE_256;
  166. break;
  167. case 8:
  168. field = DMFC_FIFO_SIZE_512;
  169. break;
  170. default:
  171. return -EINVAL;
  172. }
  173. switch (burstsize) {
  174. case 16:
  175. field |= DMFC_BURSTSIZE_16;
  176. break;
  177. case 32:
  178. field |= DMFC_BURSTSIZE_32;
  179. break;
  180. case 64:
  181. field |= DMFC_BURSTSIZE_64;
  182. break;
  183. case 128:
  184. field |= DMFC_BURSTSIZE_128;
  185. break;
  186. }
  187. field |= DMFC_SEGMENT(segment);
  188. val = readl(priv->base + dmfc->data->channel_reg);
  189. val &= ~(0xff << dmfc->data->shift);
  190. val |= field << dmfc->data->shift;
  191. writel(val, priv->base + dmfc->data->channel_reg);
  192. dmfc->slots = slots;
  193. dmfc->segment = segment;
  194. dmfc->burstsize = burstsize;
  195. dmfc->slotmask = ((1 << slots) - 1) << segment;
  196. return 0;
  197. }
  198. static int dmfc_bandwidth_to_slots(struct ipu_dmfc_priv *priv,
  199. unsigned long bandwidth)
  200. {
  201. int slots = 1;
  202. while (slots * priv->bandwidth_per_slot < bandwidth)
  203. slots *= 2;
  204. return slots;
  205. }
  206. static int dmfc_find_slots(struct ipu_dmfc_priv *priv, int slots)
  207. {
  208. unsigned slotmask_need, slotmask_used = 0;
  209. int i, segment = 0;
  210. slotmask_need = (1 << slots) - 1;
  211. for (i = 0; i < DMFC_NUM_CHANNELS; i++)
  212. slotmask_used |= priv->channels[i].slotmask;
  213. while (slotmask_need <= 0xff) {
  214. if (!(slotmask_used & slotmask_need))
  215. return segment;
  216. slotmask_need <<= 1;
  217. segment++;
  218. }
  219. return -EBUSY;
  220. }
  221. void ipu_dmfc_free_bandwidth(struct dmfc_channel *dmfc)
  222. {
  223. struct ipu_dmfc_priv *priv = dmfc->priv;
  224. int i;
  225. dev_dbg(priv->dev, "dmfc: freeing %d slots starting from segment %d\n",
  226. dmfc->slots, dmfc->segment);
  227. mutex_lock(&priv->mutex);
  228. if (!dmfc->slots)
  229. goto out;
  230. dmfc->slotmask = 0;
  231. dmfc->slots = 0;
  232. dmfc->segment = 0;
  233. for (i = 0; i < DMFC_NUM_CHANNELS; i++)
  234. priv->channels[i].slotmask = 0;
  235. for (i = 0; i < DMFC_NUM_CHANNELS; i++) {
  236. if (priv->channels[i].slots > 0) {
  237. priv->channels[i].segment =
  238. dmfc_find_slots(priv, priv->channels[i].slots);
  239. priv->channels[i].slotmask =
  240. ((1 << priv->channels[i].slots) - 1) <<
  241. priv->channels[i].segment;
  242. }
  243. }
  244. for (i = 0; i < DMFC_NUM_CHANNELS; i++) {
  245. if (priv->channels[i].slots > 0)
  246. ipu_dmfc_setup_channel(&priv->channels[i],
  247. priv->channels[i].slots,
  248. priv->channels[i].segment,
  249. priv->channels[i].burstsize);
  250. }
  251. out:
  252. mutex_unlock(&priv->mutex);
  253. }
  254. EXPORT_SYMBOL_GPL(ipu_dmfc_free_bandwidth);
  255. int ipu_dmfc_alloc_bandwidth(struct dmfc_channel *dmfc,
  256. unsigned long bandwidth_pixel_per_second, int burstsize)
  257. {
  258. struct ipu_dmfc_priv *priv = dmfc->priv;
  259. int slots = dmfc_bandwidth_to_slots(priv, bandwidth_pixel_per_second);
  260. int segment = -1, ret = 0;
  261. dev_dbg(priv->dev, "dmfc: trying to allocate %ldMpixel/s for IPU channel %d\n",
  262. bandwidth_pixel_per_second / 1000000,
  263. dmfc->data->ipu_channel);
  264. ipu_dmfc_free_bandwidth(dmfc);
  265. mutex_lock(&priv->mutex);
  266. if (slots > 8) {
  267. ret = -EBUSY;
  268. goto out;
  269. }
  270. /* For the MEM_BG channel, first try to allocate twice the slots */
  271. if (dmfc->data->ipu_channel == IPUV3_CHANNEL_MEM_BG_SYNC)
  272. segment = dmfc_find_slots(priv, slots * 2);
  273. else if (slots < 2)
  274. /* Always allocate at least 128*4 bytes (2 slots) */
  275. slots = 2;
  276. if (segment >= 0)
  277. slots *= 2;
  278. else
  279. segment = dmfc_find_slots(priv, slots);
  280. if (segment < 0) {
  281. ret = -EBUSY;
  282. goto out;
  283. }
  284. ipu_dmfc_setup_channel(dmfc, slots, segment, burstsize);
  285. out:
  286. mutex_unlock(&priv->mutex);
  287. return ret;
  288. }
  289. EXPORT_SYMBOL_GPL(ipu_dmfc_alloc_bandwidth);
  290. int ipu_dmfc_init_channel(struct dmfc_channel *dmfc, int width)
  291. {
  292. struct ipu_dmfc_priv *priv = dmfc->priv;
  293. u32 dmfc_gen1;
  294. dmfc_gen1 = readl(priv->base + DMFC_GENERAL1);
  295. if ((dmfc->slots * 64 * 4) / width > dmfc->data->max_fifo_lines)
  296. dmfc_gen1 |= 1 << dmfc->data->eot_shift;
  297. else
  298. dmfc_gen1 &= ~(1 << dmfc->data->eot_shift);
  299. writel(dmfc_gen1, priv->base + DMFC_GENERAL1);
  300. return 0;
  301. }
  302. EXPORT_SYMBOL_GPL(ipu_dmfc_init_channel);
  303. struct dmfc_channel *ipu_dmfc_get(struct ipu_soc *ipu, int ipu_channel)
  304. {
  305. struct ipu_dmfc_priv *priv = ipu->dmfc_priv;
  306. int i;
  307. for (i = 0; i < DMFC_NUM_CHANNELS; i++)
  308. if (dmfcdata[i].ipu_channel == ipu_channel)
  309. return &priv->channels[i];
  310. return ERR_PTR(-ENODEV);
  311. }
  312. EXPORT_SYMBOL_GPL(ipu_dmfc_get);
  313. void ipu_dmfc_put(struct dmfc_channel *dmfc)
  314. {
  315. ipu_dmfc_free_bandwidth(dmfc);
  316. }
  317. EXPORT_SYMBOL_GPL(ipu_dmfc_put);
  318. int ipu_dmfc_init(struct ipu_soc *ipu, struct device *dev, unsigned long base,
  319. struct clk *ipu_clk)
  320. {
  321. struct ipu_dmfc_priv *priv;
  322. int i;
  323. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  324. if (!priv)
  325. return -ENOMEM;
  326. priv->base = devm_ioremap(dev, base, PAGE_SIZE);
  327. if (!priv->base)
  328. return -ENOMEM;
  329. priv->dev = dev;
  330. priv->ipu = ipu;
  331. mutex_init(&priv->mutex);
  332. ipu->dmfc_priv = priv;
  333. for (i = 0; i < DMFC_NUM_CHANNELS; i++) {
  334. priv->channels[i].priv = priv;
  335. priv->channels[i].ipu = ipu;
  336. priv->channels[i].data = &dmfcdata[i];
  337. }
  338. writel(0x0, priv->base + DMFC_WR_CHAN);
  339. writel(0x0, priv->base + DMFC_DP_CHAN);
  340. /*
  341. * We have a total bandwidth of clkrate * 4pixel divided
  342. * into 8 slots.
  343. */
  344. priv->bandwidth_per_slot = clk_get_rate(ipu_clk) * 4 / 8;
  345. dev_dbg(dev, "dmfc: 8 slots with %ldMpixel/s bandwidth each\n",
  346. priv->bandwidth_per_slot / 1000000);
  347. writel(0x202020f6, priv->base + DMFC_WR_CHAN_DEF);
  348. writel(0x2020f6f6, priv->base + DMFC_DP_CHAN_DEF);
  349. writel(0x00000003, priv->base + DMFC_GENERAL1);
  350. return 0;
  351. }
  352. void ipu_dmfc_exit(struct ipu_soc *ipu)
  353. {
  354. }