sh_mobile_meram.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. * set the next address to fetch
  135. */
  136. static inline void meram_set_next_addr(struct sh_mobile_meram_priv *priv,
  137. struct sh_mobile_meram_cfg *cfg,
  138. unsigned long base_addr_y,
  139. unsigned long base_addr_c)
  140. {
  141. unsigned long target;
  142. target = (cfg->current_reg) ? MExxSARA : MExxSARB;
  143. cfg->current_reg ^= 1;
  144. /* set the next address to fetch */
  145. meram_write_icb(priv->base, cfg->icb[0].cache_icb, target,
  146. base_addr_y);
  147. meram_write_icb(priv->base, cfg->icb[0].marker_icb, target,
  148. base_addr_y + cfg->icb[0].cache_unit);
  149. if (cfg->pixelformat == SH_MOBILE_MERAM_PF_NV) {
  150. meram_write_icb(priv->base, cfg->icb[1].cache_icb, target,
  151. base_addr_c);
  152. meram_write_icb(priv->base, cfg->icb[1].marker_icb, target,
  153. base_addr_c + cfg->icb[1].cache_unit);
  154. }
  155. }
  156. /*
  157. * get the next ICB address
  158. */
  159. static inline void meram_get_next_icb_addr(struct sh_mobile_meram_info *pdata,
  160. struct sh_mobile_meram_cfg *cfg,
  161. unsigned long *icb_addr_y,
  162. unsigned long *icb_addr_c)
  163. {
  164. unsigned long icb_offset;
  165. if (pdata->addr_mode == SH_MOBILE_MERAM_MODE0)
  166. icb_offset = 0x80000000 | (cfg->current_reg << 29);
  167. else
  168. icb_offset = 0xc0000000 | (cfg->current_reg << 23);
  169. *icb_addr_y = icb_offset | (cfg->icb[0].marker_icb << 24);
  170. if ((*icb_addr_c) && cfg->pixelformat == SH_MOBILE_MERAM_PF_NV)
  171. *icb_addr_c = icb_offset | (cfg->icb[1].marker_icb << 24);
  172. }
  173. #define MERAM_CALC_BYTECOUNT(x, y) \
  174. (((x) * (y) + (MERAM_LINE_WIDTH - 1)) & ~(MERAM_LINE_WIDTH - 1))
  175. /*
  176. * initialize MERAM
  177. */
  178. static int meram_init(struct sh_mobile_meram_priv *priv,
  179. struct sh_mobile_meram_icb *icb,
  180. int xres, int yres, int *out_pitch)
  181. {
  182. unsigned long total_byte_count = MERAM_CALC_BYTECOUNT(xres, yres);
  183. unsigned long bnm;
  184. int lcdc_pitch, xpitch, line_cnt;
  185. int save_lines;
  186. /* adjust pitch to 1024, 2048, 4096 or 8192 */
  187. lcdc_pitch = (xres - 1) | 1023;
  188. lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 1);
  189. lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 2);
  190. lcdc_pitch += 1;
  191. /* derive settings */
  192. if (lcdc_pitch == 8192 && yres >= 1024) {
  193. lcdc_pitch = xpitch = MERAM_LINE_WIDTH;
  194. line_cnt = total_byte_count >> 11;
  195. *out_pitch = xres;
  196. save_lines = (icb->meram_size / 16 / MERAM_SEC_LINE);
  197. save_lines *= MERAM_SEC_LINE;
  198. } else {
  199. xpitch = xres;
  200. line_cnt = yres;
  201. *out_pitch = lcdc_pitch;
  202. save_lines = icb->meram_size / (lcdc_pitch >> 10) / 2;
  203. save_lines &= 0xff;
  204. }
  205. bnm = (save_lines - 1) << 16;
  206. /* TODO: we better to check if we have enough MERAM buffer size */
  207. /* set up ICB */
  208. meram_write_icb(priv->base, icb->cache_icb, MExxBSIZE,
  209. MERAM_MExxBSIZE_VAL(0x0, line_cnt - 1, xpitch - 1));
  210. meram_write_icb(priv->base, icb->marker_icb, MExxBSIZE,
  211. MERAM_MExxBSIZE_VAL(0xf, line_cnt - 1, xpitch - 1));
  212. meram_write_icb(priv->base, icb->cache_icb, MExxMNCF, bnm);
  213. meram_write_icb(priv->base, icb->marker_icb, MExxMNCF, bnm);
  214. meram_write_icb(priv->base, icb->cache_icb, MExxSBSIZE, xpitch);
  215. meram_write_icb(priv->base, icb->marker_icb, MExxSBSIZE, xpitch);
  216. /* save a cache unit size */
  217. icb->cache_unit = xres * save_lines;
  218. /*
  219. * Set MERAM for framebuffer
  220. *
  221. * 0x70f: WD = 0x3, WS=0x1, CM=0x1, MD=FB mode
  222. * we also chain the cache_icb and the marker_icb.
  223. * we also split the allocated MERAM buffer between two ICBs.
  224. */
  225. meram_write_icb(priv->base, icb->cache_icb, MExxCTL,
  226. MERAM_MExxCTL_VAL(0x70f, icb->marker_icb,
  227. icb->meram_offset));
  228. meram_write_icb(priv->base, icb->marker_icb, MExxCTL,
  229. MERAM_MExxCTL_VAL(0x70f, icb->cache_icb,
  230. icb->meram_offset +
  231. icb->meram_size / 2));
  232. return 0;
  233. }
  234. static void meram_deinit(struct sh_mobile_meram_priv *priv,
  235. struct sh_mobile_meram_icb *icb)
  236. {
  237. /* disable ICB */
  238. meram_write_icb(priv->base, icb->cache_icb, MExxCTL, 0);
  239. meram_write_icb(priv->base, icb->marker_icb, MExxCTL, 0);
  240. icb->cache_unit = 0;
  241. }
  242. /*
  243. * register the ICB
  244. */
  245. static int sh_mobile_meram_register(struct sh_mobile_meram_info *pdata,
  246. struct sh_mobile_meram_cfg *cfg,
  247. int xres, int yres, int pixelformat,
  248. unsigned long base_addr_y,
  249. unsigned long base_addr_c,
  250. unsigned long *icb_addr_y,
  251. unsigned long *icb_addr_c,
  252. int *pitch)
  253. {
  254. struct platform_device *pdev;
  255. struct sh_mobile_meram_priv *priv;
  256. int n, out_pitch;
  257. int error = 0;
  258. if (!pdata || !pdata->priv || !pdata->pdev || !cfg)
  259. return -EINVAL;
  260. if (pixelformat != SH_MOBILE_MERAM_PF_NV &&
  261. pixelformat != SH_MOBILE_MERAM_PF_RGB)
  262. return -EINVAL;
  263. priv = pdata->priv;
  264. pdev = pdata->pdev;
  265. dev_dbg(&pdev->dev, "registering %dx%d (%s) (y=%08lx, c=%08lx)",
  266. xres, yres, (!pixelformat) ? "yuv" : "rgb",
  267. base_addr_y, base_addr_c);
  268. mutex_lock(&priv->lock);
  269. /* we can't handle wider than 8192px */
  270. if (xres > 8192) {
  271. dev_err(&pdev->dev, "width exceeding the limit (> 8192).");
  272. error = -EINVAL;
  273. goto err;
  274. }
  275. if (priv->used_meram_cache_regions + 2 > SH_MOBILE_MERAM_ICB_NUM) {
  276. dev_err(&pdev->dev, "no more ICB available.");
  277. error = -EINVAL;
  278. goto err;
  279. }
  280. /* do we have at least one ICB config? */
  281. if (cfg->icb[0].marker_icb < 0 || cfg->icb[0].cache_icb < 0) {
  282. dev_err(&pdev->dev, "at least one ICB is required.");
  283. error = -EINVAL;
  284. goto err;
  285. }
  286. /* make sure that there's no overlaps */
  287. if (meram_check_overlap(priv, &cfg->icb[0])) {
  288. dev_err(&pdev->dev, "conflicting config detected.");
  289. error = -EINVAL;
  290. goto err;
  291. }
  292. n = 1;
  293. /* do the same if we have the second ICB set */
  294. if (cfg->icb[1].marker_icb >= 0 && cfg->icb[1].cache_icb >= 0) {
  295. if (meram_check_overlap(priv, &cfg->icb[1])) {
  296. dev_err(&pdev->dev, "conflicting config detected.");
  297. error = -EINVAL;
  298. goto err;
  299. }
  300. n = 2;
  301. }
  302. if (pixelformat == SH_MOBILE_MERAM_PF_NV && n != 2) {
  303. dev_err(&pdev->dev, "requires two ICB sets for planar Y/C.");
  304. error = -EINVAL;
  305. goto err;
  306. }
  307. /* we now register the ICB */
  308. cfg->pixelformat = pixelformat;
  309. meram_mark(priv, &cfg->icb[0]);
  310. if (pixelformat == SH_MOBILE_MERAM_PF_NV)
  311. meram_mark(priv, &cfg->icb[1]);
  312. /* initialize MERAM */
  313. meram_init(priv, &cfg->icb[0], xres, yres, &out_pitch);
  314. *pitch = out_pitch;
  315. if (pixelformat == SH_MOBILE_MERAM_PF_NV)
  316. meram_init(priv, &cfg->icb[1], xres, (yres + 1) / 2,
  317. &out_pitch);
  318. cfg->current_reg = 1;
  319. meram_set_next_addr(priv, cfg, base_addr_y, base_addr_c);
  320. meram_get_next_icb_addr(pdata, cfg, icb_addr_y, icb_addr_c);
  321. dev_dbg(&pdev->dev, "registered - can access via y=%08lx, c=%08lx",
  322. *icb_addr_y, *icb_addr_c);
  323. err:
  324. mutex_unlock(&priv->lock);
  325. return error;
  326. }
  327. static int sh_mobile_meram_unregister(struct sh_mobile_meram_info *pdata,
  328. struct sh_mobile_meram_cfg *cfg)
  329. {
  330. struct sh_mobile_meram_priv *priv;
  331. if (!pdata || !pdata->priv || !cfg)
  332. return -EINVAL;
  333. priv = pdata->priv;
  334. mutex_lock(&priv->lock);
  335. /* deinit & unmark */
  336. if (cfg->pixelformat == SH_MOBILE_MERAM_PF_NV) {
  337. meram_deinit(priv, &cfg->icb[1]);
  338. meram_unmark(priv, &cfg->icb[1]);
  339. }
  340. meram_deinit(priv, &cfg->icb[0]);
  341. meram_unmark(priv, &cfg->icb[0]);
  342. mutex_unlock(&priv->lock);
  343. return 0;
  344. }
  345. static int sh_mobile_meram_update(struct sh_mobile_meram_info *pdata,
  346. struct sh_mobile_meram_cfg *cfg,
  347. unsigned long base_addr_y,
  348. unsigned long base_addr_c,
  349. unsigned long *icb_addr_y,
  350. unsigned long *icb_addr_c)
  351. {
  352. struct sh_mobile_meram_priv *priv;
  353. if (!pdata || !pdata->priv || !cfg)
  354. return -EINVAL;
  355. priv = pdata->priv;
  356. mutex_lock(&priv->lock);
  357. meram_set_next_addr(priv, cfg, base_addr_y, base_addr_c);
  358. meram_get_next_icb_addr(pdata, cfg, icb_addr_y, icb_addr_c);
  359. mutex_unlock(&priv->lock);
  360. return 0;
  361. }
  362. static struct sh_mobile_meram_ops sh_mobile_meram_ops = {
  363. .module = THIS_MODULE,
  364. .meram_register = sh_mobile_meram_register,
  365. .meram_unregister = sh_mobile_meram_unregister,
  366. .meram_update = sh_mobile_meram_update,
  367. };
  368. /*
  369. * initialize MERAM
  370. */
  371. static int sh_mobile_meram_remove(struct platform_device *pdev);
  372. static int __devinit sh_mobile_meram_probe(struct platform_device *pdev)
  373. {
  374. struct sh_mobile_meram_priv *priv;
  375. struct sh_mobile_meram_info *pdata = pdev->dev.platform_data;
  376. struct resource *res;
  377. int error;
  378. if (!pdata) {
  379. dev_err(&pdev->dev, "no platform data defined\n");
  380. return -EINVAL;
  381. }
  382. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  383. if (!res) {
  384. dev_err(&pdev->dev, "cannot get platform resources\n");
  385. return -ENOENT;
  386. }
  387. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  388. if (!priv) {
  389. dev_err(&pdev->dev, "cannot allocate device data\n");
  390. return -ENOMEM;
  391. }
  392. platform_set_drvdata(pdev, priv);
  393. /* initialize private data */
  394. mutex_init(&priv->lock);
  395. priv->base = ioremap_nocache(res->start, resource_size(res));
  396. if (!priv->base) {
  397. dev_err(&pdev->dev, "ioremap failed\n");
  398. error = -EFAULT;
  399. goto err;
  400. }
  401. pdata->ops = &sh_mobile_meram_ops;
  402. pdata->priv = priv;
  403. pdata->pdev = pdev;
  404. /* initialize ICB addressing mode */
  405. if (pdata->addr_mode == SH_MOBILE_MERAM_MODE1)
  406. meram_write_reg(priv->base, MEVCR1, 1 << 29);
  407. dev_info(&pdev->dev, "sh_mobile_meram initialized.");
  408. return 0;
  409. err:
  410. sh_mobile_meram_remove(pdev);
  411. return error;
  412. }
  413. static int sh_mobile_meram_remove(struct platform_device *pdev)
  414. {
  415. struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev);
  416. if (priv->base)
  417. iounmap(priv->base);
  418. mutex_destroy(&priv->lock);
  419. kfree(priv);
  420. return 0;
  421. }
  422. static struct platform_driver sh_mobile_meram_driver = {
  423. .driver = {
  424. .name = "sh_mobile_meram",
  425. .owner = THIS_MODULE,
  426. },
  427. .probe = sh_mobile_meram_probe,
  428. .remove = sh_mobile_meram_remove,
  429. };
  430. static int __init sh_mobile_meram_init(void)
  431. {
  432. return platform_driver_register(&sh_mobile_meram_driver);
  433. }
  434. static void __exit sh_mobile_meram_exit(void)
  435. {
  436. platform_driver_unregister(&sh_mobile_meram_driver);
  437. }
  438. module_init(sh_mobile_meram_init);
  439. module_exit(sh_mobile_meram_exit);
  440. MODULE_DESCRIPTION("SuperH Mobile MERAM driver");
  441. MODULE_AUTHOR("Damian Hobson-Garcia / Takanari Hayama");
  442. MODULE_LICENSE("GPL v2");