sh_mobile_meram.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * SuperH Mobile MERAM Driver for SuperH Mobile LCDC Driver
  3. *
  4. * Copyright (c) 2011 Damian Hobson-Garcia <dhobsong@igel.co.jp>
  5. * Takanari Hayama <taki@igel.co.jp>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/device.h>
  14. #include <linux/io.h>
  15. #include <linux/slab.h>
  16. #include <linux/platform_device.h>
  17. #include "sh_mobile_meram.h"
  18. /* meram registers */
  19. #define MExxCTL 0x0
  20. #define MExxBSIZE 0x4
  21. #define MExxMNCF 0x8
  22. #define MExxSARA 0x10
  23. #define MExxSARB 0x14
  24. #define MExxSBSIZE 0x18
  25. #define MERAM_MExxCTL_VAL(ctl, next_icb, addr) \
  26. ((ctl) | (((next_icb) & 0x1f) << 11) | (((addr) & 0x7ff) << 16))
  27. #define MERAM_MExxBSIZE_VAL(a, b, c) \
  28. (((a) << 28) | ((b) << 16) | (c))
  29. #define MEVCR1 0x4
  30. #define MEACTS 0x10
  31. #define MEQSEL1 0x40
  32. #define MEQSEL2 0x44
  33. /* settings */
  34. #define MERAM_SEC_LINE 15
  35. #define MERAM_LINE_WIDTH 2048
  36. /*
  37. * MERAM/ICB access functions
  38. */
  39. #define MERAM_ICB_OFFSET(base, idx, off) \
  40. ((base) + (0x400 + ((idx) * 0x20) + (off)))
  41. static inline void meram_write_icb(void __iomem *base, int idx, int off,
  42. unsigned long val)
  43. {
  44. iowrite32(val, MERAM_ICB_OFFSET(base, idx, off));
  45. }
  46. static inline unsigned long meram_read_icb(void __iomem *base, int idx, int off)
  47. {
  48. return ioread32(MERAM_ICB_OFFSET(base, idx, off));
  49. }
  50. static inline void meram_write_reg(void __iomem *base, int off,
  51. unsigned long val)
  52. {
  53. iowrite32(val, base + off);
  54. }
  55. static inline unsigned long meram_read_reg(void __iomem *base, int off)
  56. {
  57. return ioread32(base + off);
  58. }
  59. /*
  60. * register ICB
  61. */
  62. #define MERAM_CACHE_START(p) ((p) >> 16)
  63. #define MERAM_CACHE_END(p) ((p) & 0xffff)
  64. #define MERAM_CACHE_SET(o, s) ((((o) & 0xffff) << 16) | \
  65. (((o) + (s) - 1) & 0xffff))
  66. /*
  67. * check if there's no overlaps in MERAM allocation.
  68. */
  69. static inline int meram_check_overlap(struct sh_mobile_meram_priv *priv,
  70. struct sh_mobile_meram_icb *new)
  71. {
  72. int i;
  73. int used_start, used_end, meram_start, meram_end;
  74. /* valid ICB? */
  75. if (new->marker_icb & ~0x1f || new->cache_icb & ~0x1f)
  76. return 1;
  77. if (test_bit(new->marker_icb, &priv->used_icb) ||
  78. test_bit(new->cache_icb, &priv->used_icb))
  79. return 1;
  80. for (i = 0; i < priv->used_meram_cache_regions; i++) {
  81. used_start = MERAM_CACHE_START(priv->used_meram_cache[i]);
  82. used_end = MERAM_CACHE_END(priv->used_meram_cache[i]);
  83. meram_start = new->meram_offset;
  84. meram_end = new->meram_offset + new->meram_size;
  85. if ((meram_start >= used_start && meram_start < used_end) ||
  86. (meram_end > used_start && meram_end < used_end))
  87. return 1;
  88. }
  89. return 0;
  90. }
  91. /*
  92. * mark the specified ICB as used
  93. */
  94. static inline void meram_mark(struct sh_mobile_meram_priv *priv,
  95. struct sh_mobile_meram_icb *new)
  96. {
  97. int n;
  98. if (new->marker_icb < 0 || new->cache_icb < 0)
  99. return;
  100. __set_bit(new->marker_icb, &priv->used_icb);
  101. __set_bit(new->cache_icb, &priv->used_icb);
  102. n = priv->used_meram_cache_regions;
  103. priv->used_meram_cache[n] = MERAM_CACHE_SET(new->meram_offset,
  104. new->meram_size);
  105. priv->used_meram_cache_regions++;
  106. }
  107. /*
  108. * unmark the specified ICB as used
  109. */
  110. static inline void meram_unmark(struct sh_mobile_meram_priv *priv,
  111. struct sh_mobile_meram_icb *icb)
  112. {
  113. int i;
  114. unsigned long pattern;
  115. if (icb->marker_icb < 0 || icb->cache_icb < 0)
  116. return;
  117. __clear_bit(icb->marker_icb, &priv->used_icb);
  118. __clear_bit(icb->cache_icb, &priv->used_icb);
  119. pattern = MERAM_CACHE_SET(icb->meram_offset, icb->meram_size);
  120. for (i = 0; i < priv->used_meram_cache_regions; i++) {
  121. if (priv->used_meram_cache[i] == pattern) {
  122. while (i < priv->used_meram_cache_regions - 1) {
  123. priv->used_meram_cache[i] =
  124. priv->used_meram_cache[i + 1] ;
  125. i++;
  126. }
  127. priv->used_meram_cache[i] = 0;
  128. priv->used_meram_cache_regions--;
  129. break;
  130. }
  131. }
  132. }
  133. /*
  134. * is this a YCbCr(NV12, NV16 or NV24) colorspace
  135. */
  136. static inline int is_nvcolor(int cspace)
  137. {
  138. if (cspace == SH_MOBILE_MERAM_PF_NV ||
  139. cspace == SH_MOBILE_MERAM_PF_NV24)
  140. return 1;
  141. return 0;
  142. }
  143. /*
  144. * set the next address to fetch
  145. */
  146. static inline void meram_set_next_addr(struct sh_mobile_meram_priv *priv,
  147. struct sh_mobile_meram_cfg *cfg,
  148. unsigned long base_addr_y,
  149. unsigned long base_addr_c)
  150. {
  151. unsigned long target;
  152. target = (cfg->current_reg) ? MExxSARA : MExxSARB;
  153. cfg->current_reg ^= 1;
  154. /* set the next address to fetch */
  155. meram_write_icb(priv->base, cfg->icb[0].cache_icb, target,
  156. base_addr_y);
  157. meram_write_icb(priv->base, cfg->icb[0].marker_icb, target,
  158. base_addr_y + cfg->icb[0].cache_unit);
  159. if (is_nvcolor(cfg->pixelformat)) {
  160. meram_write_icb(priv->base, cfg->icb[1].cache_icb, target,
  161. base_addr_c);
  162. meram_write_icb(priv->base, cfg->icb[1].marker_icb, target,
  163. base_addr_c + cfg->icb[1].cache_unit);
  164. }
  165. }
  166. /*
  167. * get the next ICB address
  168. */
  169. static inline void meram_get_next_icb_addr(struct sh_mobile_meram_info *pdata,
  170. struct sh_mobile_meram_cfg *cfg,
  171. unsigned long *icb_addr_y,
  172. unsigned long *icb_addr_c)
  173. {
  174. unsigned long icb_offset;
  175. if (pdata->addr_mode == SH_MOBILE_MERAM_MODE0)
  176. icb_offset = 0x80000000 | (cfg->current_reg << 29);
  177. else
  178. icb_offset = 0xc0000000 | (cfg->current_reg << 23);
  179. *icb_addr_y = icb_offset | (cfg->icb[0].marker_icb << 24);
  180. if (is_nvcolor(cfg->pixelformat))
  181. *icb_addr_c = icb_offset | (cfg->icb[1].marker_icb << 24);
  182. }
  183. #define MERAM_CALC_BYTECOUNT(x, y) \
  184. (((x) * (y) + (MERAM_LINE_WIDTH - 1)) & ~(MERAM_LINE_WIDTH - 1))
  185. /*
  186. * initialize MERAM
  187. */
  188. static int meram_init(struct sh_mobile_meram_priv *priv,
  189. struct sh_mobile_meram_icb *icb,
  190. int xres, int yres, int *out_pitch)
  191. {
  192. unsigned long total_byte_count = MERAM_CALC_BYTECOUNT(xres, yres);
  193. unsigned long bnm;
  194. int lcdc_pitch, xpitch, line_cnt;
  195. int save_lines;
  196. /* adjust pitch to 1024, 2048, 4096 or 8192 */
  197. lcdc_pitch = (xres - 1) | 1023;
  198. lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 1);
  199. lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 2);
  200. lcdc_pitch += 1;
  201. /* derive settings */
  202. if (lcdc_pitch == 8192 && yres >= 1024) {
  203. lcdc_pitch = xpitch = MERAM_LINE_WIDTH;
  204. line_cnt = total_byte_count >> 11;
  205. *out_pitch = xres;
  206. save_lines = (icb->meram_size / 16 / MERAM_SEC_LINE);
  207. save_lines *= MERAM_SEC_LINE;
  208. } else {
  209. xpitch = xres;
  210. line_cnt = yres;
  211. *out_pitch = lcdc_pitch;
  212. save_lines = icb->meram_size / (lcdc_pitch >> 10) / 2;
  213. save_lines &= 0xff;
  214. }
  215. bnm = (save_lines - 1) << 16;
  216. /* TODO: we better to check if we have enough MERAM buffer size */
  217. /* set up ICB */
  218. meram_write_icb(priv->base, icb->cache_icb, MExxBSIZE,
  219. MERAM_MExxBSIZE_VAL(0x0, line_cnt - 1, xpitch - 1));
  220. meram_write_icb(priv->base, icb->marker_icb, MExxBSIZE,
  221. MERAM_MExxBSIZE_VAL(0xf, line_cnt - 1, xpitch - 1));
  222. meram_write_icb(priv->base, icb->cache_icb, MExxMNCF, bnm);
  223. meram_write_icb(priv->base, icb->marker_icb, MExxMNCF, bnm);
  224. meram_write_icb(priv->base, icb->cache_icb, MExxSBSIZE, xpitch);
  225. meram_write_icb(priv->base, icb->marker_icb, MExxSBSIZE, xpitch);
  226. /* save a cache unit size */
  227. icb->cache_unit = xres * save_lines;
  228. /*
  229. * Set MERAM for framebuffer
  230. *
  231. * 0x70f: WD = 0x3, WS=0x1, CM=0x1, MD=FB mode
  232. * we also chain the cache_icb and the marker_icb.
  233. * we also split the allocated MERAM buffer between two ICBs.
  234. */
  235. meram_write_icb(priv->base, icb->cache_icb, MExxCTL,
  236. MERAM_MExxCTL_VAL(0x70f, icb->marker_icb,
  237. icb->meram_offset));
  238. meram_write_icb(priv->base, icb->marker_icb, MExxCTL,
  239. MERAM_MExxCTL_VAL(0x70f, icb->cache_icb,
  240. icb->meram_offset +
  241. icb->meram_size / 2));
  242. return 0;
  243. }
  244. static void meram_deinit(struct sh_mobile_meram_priv *priv,
  245. struct sh_mobile_meram_icb *icb)
  246. {
  247. /* disable ICB */
  248. meram_write_icb(priv->base, icb->cache_icb, MExxCTL, 0);
  249. meram_write_icb(priv->base, icb->marker_icb, MExxCTL, 0);
  250. icb->cache_unit = 0;
  251. }
  252. /*
  253. * register the ICB
  254. */
  255. static int sh_mobile_meram_register(struct sh_mobile_meram_info *pdata,
  256. struct sh_mobile_meram_cfg *cfg,
  257. int xres, int yres, int pixelformat,
  258. unsigned long base_addr_y,
  259. unsigned long base_addr_c,
  260. unsigned long *icb_addr_y,
  261. unsigned long *icb_addr_c,
  262. int *pitch)
  263. {
  264. struct platform_device *pdev;
  265. struct sh_mobile_meram_priv *priv;
  266. int n, out_pitch;
  267. int error = 0;
  268. if (!pdata || !pdata->priv || !pdata->pdev || !cfg)
  269. return -EINVAL;
  270. if (pixelformat != SH_MOBILE_MERAM_PF_NV &&
  271. pixelformat != SH_MOBILE_MERAM_PF_NV24 &&
  272. pixelformat != SH_MOBILE_MERAM_PF_RGB)
  273. return -EINVAL;
  274. priv = pdata->priv;
  275. pdev = pdata->pdev;
  276. dev_dbg(&pdev->dev, "registering %dx%d (%s) (y=%08lx, c=%08lx)",
  277. xres, yres, (!pixelformat) ? "yuv" : "rgb",
  278. base_addr_y, base_addr_c);
  279. mutex_lock(&priv->lock);
  280. /* we can't handle wider than 8192px */
  281. if (xres > 8192) {
  282. dev_err(&pdev->dev, "width exceeding the limit (> 8192).");
  283. error = -EINVAL;
  284. goto err;
  285. }
  286. if (priv->used_meram_cache_regions + 2 > SH_MOBILE_MERAM_ICB_NUM) {
  287. dev_err(&pdev->dev, "no more ICB available.");
  288. error = -EINVAL;
  289. goto err;
  290. }
  291. /* do we have at least one ICB config? */
  292. if (cfg->icb[0].marker_icb < 0 || cfg->icb[0].cache_icb < 0) {
  293. dev_err(&pdev->dev, "at least one ICB is required.");
  294. error = -EINVAL;
  295. goto err;
  296. }
  297. /* make sure that there's no overlaps */
  298. if (meram_check_overlap(priv, &cfg->icb[0])) {
  299. dev_err(&pdev->dev, "conflicting config detected.");
  300. error = -EINVAL;
  301. goto err;
  302. }
  303. n = 1;
  304. /* do the same if we have the second ICB set */
  305. if (cfg->icb[1].marker_icb >= 0 && cfg->icb[1].cache_icb >= 0) {
  306. if (meram_check_overlap(priv, &cfg->icb[1])) {
  307. dev_err(&pdev->dev, "conflicting config detected.");
  308. error = -EINVAL;
  309. goto err;
  310. }
  311. n = 2;
  312. }
  313. if (is_nvcolor(pixelformat) && n != 2) {
  314. dev_err(&pdev->dev, "requires two ICB sets for planar Y/C.");
  315. error = -EINVAL;
  316. goto err;
  317. }
  318. /* we now register the ICB */
  319. cfg->pixelformat = pixelformat;
  320. meram_mark(priv, &cfg->icb[0]);
  321. if (is_nvcolor(pixelformat))
  322. meram_mark(priv, &cfg->icb[1]);
  323. /* initialize MERAM */
  324. meram_init(priv, &cfg->icb[0], xres, yres, &out_pitch);
  325. *pitch = out_pitch;
  326. if (pixelformat == SH_MOBILE_MERAM_PF_NV)
  327. meram_init(priv, &cfg->icb[1], xres, (yres + 1) / 2,
  328. &out_pitch);
  329. else if (pixelformat == SH_MOBILE_MERAM_PF_NV24)
  330. meram_init(priv, &cfg->icb[1], 2 * xres, (yres + 1) / 2,
  331. &out_pitch);
  332. cfg->current_reg = 1;
  333. meram_set_next_addr(priv, cfg, base_addr_y, base_addr_c);
  334. meram_get_next_icb_addr(pdata, cfg, icb_addr_y, icb_addr_c);
  335. dev_dbg(&pdev->dev, "registered - can access via y=%08lx, c=%08lx",
  336. *icb_addr_y, *icb_addr_c);
  337. err:
  338. mutex_unlock(&priv->lock);
  339. return error;
  340. }
  341. static int sh_mobile_meram_unregister(struct sh_mobile_meram_info *pdata,
  342. struct sh_mobile_meram_cfg *cfg)
  343. {
  344. struct sh_mobile_meram_priv *priv;
  345. if (!pdata || !pdata->priv || !cfg)
  346. return -EINVAL;
  347. priv = pdata->priv;
  348. mutex_lock(&priv->lock);
  349. /* deinit & unmark */
  350. if (is_nvcolor(cfg->pixelformat)) {
  351. meram_deinit(priv, &cfg->icb[1]);
  352. meram_unmark(priv, &cfg->icb[1]);
  353. }
  354. meram_deinit(priv, &cfg->icb[0]);
  355. meram_unmark(priv, &cfg->icb[0]);
  356. mutex_unlock(&priv->lock);
  357. return 0;
  358. }
  359. static int sh_mobile_meram_update(struct sh_mobile_meram_info *pdata,
  360. struct sh_mobile_meram_cfg *cfg,
  361. unsigned long base_addr_y,
  362. unsigned long base_addr_c,
  363. unsigned long *icb_addr_y,
  364. unsigned long *icb_addr_c)
  365. {
  366. struct sh_mobile_meram_priv *priv;
  367. if (!pdata || !pdata->priv || !cfg)
  368. return -EINVAL;
  369. priv = pdata->priv;
  370. mutex_lock(&priv->lock);
  371. meram_set_next_addr(priv, cfg, base_addr_y, base_addr_c);
  372. meram_get_next_icb_addr(pdata, cfg, icb_addr_y, icb_addr_c);
  373. mutex_unlock(&priv->lock);
  374. return 0;
  375. }
  376. static struct sh_mobile_meram_ops sh_mobile_meram_ops = {
  377. .module = THIS_MODULE,
  378. .meram_register = sh_mobile_meram_register,
  379. .meram_unregister = sh_mobile_meram_unregister,
  380. .meram_update = sh_mobile_meram_update,
  381. };
  382. /*
  383. * initialize MERAM
  384. */
  385. static int sh_mobile_meram_remove(struct platform_device *pdev);
  386. static int __devinit sh_mobile_meram_probe(struct platform_device *pdev)
  387. {
  388. struct sh_mobile_meram_priv *priv;
  389. struct sh_mobile_meram_info *pdata = pdev->dev.platform_data;
  390. struct resource *res;
  391. int error;
  392. if (!pdata) {
  393. dev_err(&pdev->dev, "no platform data defined\n");
  394. return -EINVAL;
  395. }
  396. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  397. if (!res) {
  398. dev_err(&pdev->dev, "cannot get platform resources\n");
  399. return -ENOENT;
  400. }
  401. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  402. if (!priv) {
  403. dev_err(&pdev->dev, "cannot allocate device data\n");
  404. return -ENOMEM;
  405. }
  406. platform_set_drvdata(pdev, priv);
  407. /* initialize private data */
  408. mutex_init(&priv->lock);
  409. priv->base = ioremap_nocache(res->start, resource_size(res));
  410. if (!priv->base) {
  411. dev_err(&pdev->dev, "ioremap failed\n");
  412. error = -EFAULT;
  413. goto err;
  414. }
  415. pdata->ops = &sh_mobile_meram_ops;
  416. pdata->priv = priv;
  417. pdata->pdev = pdev;
  418. /* initialize ICB addressing mode */
  419. if (pdata->addr_mode == SH_MOBILE_MERAM_MODE1)
  420. meram_write_reg(priv->base, MEVCR1, 1 << 29);
  421. dev_info(&pdev->dev, "sh_mobile_meram initialized.");
  422. return 0;
  423. err:
  424. sh_mobile_meram_remove(pdev);
  425. return error;
  426. }
  427. static int sh_mobile_meram_remove(struct platform_device *pdev)
  428. {
  429. struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev);
  430. if (priv->base)
  431. iounmap(priv->base);
  432. mutex_destroy(&priv->lock);
  433. kfree(priv);
  434. return 0;
  435. }
  436. static struct platform_driver sh_mobile_meram_driver = {
  437. .driver = {
  438. .name = "sh_mobile_meram",
  439. .owner = THIS_MODULE,
  440. },
  441. .probe = sh_mobile_meram_probe,
  442. .remove = sh_mobile_meram_remove,
  443. };
  444. static int __init sh_mobile_meram_init(void)
  445. {
  446. return platform_driver_register(&sh_mobile_meram_driver);
  447. }
  448. static void __exit sh_mobile_meram_exit(void)
  449. {
  450. platform_driver_unregister(&sh_mobile_meram_driver);
  451. }
  452. module_init(sh_mobile_meram_init);
  453. module_exit(sh_mobile_meram_exit);
  454. MODULE_DESCRIPTION("SuperH Mobile MERAM driver");
  455. MODULE_AUTHOR("Damian Hobson-Garcia / Takanari Hayama");
  456. MODULE_LICENSE("GPL v2");