sh_mobile_meram.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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/device.h>
  12. #include <linux/err.h>
  13. #include <linux/export.h>
  14. #include <linux/genalloc.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/slab.h>
  21. #include <video/sh_mobile_meram.h>
  22. /* -----------------------------------------------------------------------------
  23. * MERAM registers
  24. */
  25. #define MEVCR1 0x4
  26. #define MEVCR1_RST (1 << 31)
  27. #define MEVCR1_WD (1 << 30)
  28. #define MEVCR1_AMD1 (1 << 29)
  29. #define MEVCR1_AMD0 (1 << 28)
  30. #define MEQSEL1 0x40
  31. #define MEQSEL2 0x44
  32. #define MExxCTL 0x400
  33. #define MExxCTL_BV (1 << 31)
  34. #define MExxCTL_BSZ_SHIFT 28
  35. #define MExxCTL_MSAR_MASK (0x7ff << MExxCTL_MSAR_SHIFT)
  36. #define MExxCTL_MSAR_SHIFT 16
  37. #define MExxCTL_NXT_MASK (0x1f << MExxCTL_NXT_SHIFT)
  38. #define MExxCTL_NXT_SHIFT 11
  39. #define MExxCTL_WD1 (1 << 10)
  40. #define MExxCTL_WD0 (1 << 9)
  41. #define MExxCTL_WS (1 << 8)
  42. #define MExxCTL_CB (1 << 7)
  43. #define MExxCTL_WBF (1 << 6)
  44. #define MExxCTL_WF (1 << 5)
  45. #define MExxCTL_RF (1 << 4)
  46. #define MExxCTL_CM (1 << 3)
  47. #define MExxCTL_MD_READ (1 << 0)
  48. #define MExxCTL_MD_WRITE (2 << 0)
  49. #define MExxCTL_MD_ICB_WB (3 << 0)
  50. #define MExxCTL_MD_ICB (4 << 0)
  51. #define MExxCTL_MD_FB (7 << 0)
  52. #define MExxCTL_MD_MASK (7 << 0)
  53. #define MExxBSIZE 0x404
  54. #define MExxBSIZE_RCNT_SHIFT 28
  55. #define MExxBSIZE_YSZM1_SHIFT 16
  56. #define MExxBSIZE_XSZM1_SHIFT 0
  57. #define MExxMNCF 0x408
  58. #define MExxMNCF_KWBNM_SHIFT 28
  59. #define MExxMNCF_KRBNM_SHIFT 24
  60. #define MExxMNCF_BNM_SHIFT 16
  61. #define MExxMNCF_XBV (1 << 15)
  62. #define MExxMNCF_CPL_YCBCR444 (1 << 12)
  63. #define MExxMNCF_CPL_YCBCR420 (2 << 12)
  64. #define MExxMNCF_CPL_YCBCR422 (3 << 12)
  65. #define MExxMNCF_CPL_MSK (3 << 12)
  66. #define MExxMNCF_BL (1 << 2)
  67. #define MExxMNCF_LNM_SHIFT 0
  68. #define MExxSARA 0x410
  69. #define MExxSARB 0x414
  70. #define MExxSBSIZE 0x418
  71. #define MExxSBSIZE_HDV (1 << 31)
  72. #define MExxSBSIZE_HSZ16 (0 << 28)
  73. #define MExxSBSIZE_HSZ32 (1 << 28)
  74. #define MExxSBSIZE_HSZ64 (2 << 28)
  75. #define MExxSBSIZE_HSZ128 (3 << 28)
  76. #define MExxSBSIZE_SBSIZZ_SHIFT 0
  77. #define MERAM_MExxCTL_VAL(next, addr) \
  78. ((((next) << MExxCTL_NXT_SHIFT) & MExxCTL_NXT_MASK) | \
  79. (((addr) << MExxCTL_MSAR_SHIFT) & MExxCTL_MSAR_MASK))
  80. #define MERAM_MExxBSIZE_VAL(rcnt, yszm1, xszm1) \
  81. (((rcnt) << MExxBSIZE_RCNT_SHIFT) | \
  82. ((yszm1) << MExxBSIZE_YSZM1_SHIFT) | \
  83. ((xszm1) << MExxBSIZE_XSZM1_SHIFT))
  84. static const unsigned long common_regs[] = {
  85. MEVCR1,
  86. MEQSEL1,
  87. MEQSEL2,
  88. };
  89. #define MERAM_REGS_SIZE ARRAY_SIZE(common_regs)
  90. static const unsigned long icb_regs[] = {
  91. MExxCTL,
  92. MExxBSIZE,
  93. MExxMNCF,
  94. MExxSARA,
  95. MExxSARB,
  96. MExxSBSIZE,
  97. };
  98. #define ICB_REGS_SIZE ARRAY_SIZE(icb_regs)
  99. /*
  100. * sh_mobile_meram_icb - MERAM ICB information
  101. * @regs: Registers cache
  102. * @index: ICB index
  103. * @offset: MERAM block offset
  104. * @size: MERAM block size in KiB
  105. * @cache_unit: Bytes to cache per ICB
  106. * @pixelformat: Video pixel format of the data stored in the ICB
  107. * @current_reg: Which of Start Address Register A (0) or B (1) is in use
  108. */
  109. struct sh_mobile_meram_icb {
  110. unsigned long regs[ICB_REGS_SIZE];
  111. unsigned int index;
  112. unsigned long offset;
  113. unsigned int size;
  114. unsigned int cache_unit;
  115. unsigned int pixelformat;
  116. unsigned int current_reg;
  117. };
  118. #define MERAM_ICB_NUM 32
  119. struct sh_mobile_meram_fb_plane {
  120. struct sh_mobile_meram_icb *marker;
  121. struct sh_mobile_meram_icb *cache;
  122. };
  123. struct sh_mobile_meram_fb_cache {
  124. unsigned int nplanes;
  125. struct sh_mobile_meram_fb_plane planes[2];
  126. };
  127. /*
  128. * sh_mobile_meram_priv - MERAM device
  129. * @base: Registers base address
  130. * @meram: MERAM physical address
  131. * @regs: Registers cache
  132. * @lock: Protects used_icb and icbs
  133. * @used_icb: Bitmask of used ICBs
  134. * @icbs: ICBs
  135. * @pool: Allocation pool to manage the MERAM
  136. */
  137. struct sh_mobile_meram_priv {
  138. void __iomem *base;
  139. unsigned long meram;
  140. unsigned long regs[MERAM_REGS_SIZE];
  141. struct mutex lock;
  142. unsigned long used_icb;
  143. struct sh_mobile_meram_icb icbs[MERAM_ICB_NUM];
  144. struct gen_pool *pool;
  145. };
  146. /* settings */
  147. #define MERAM_GRANULARITY 1024
  148. #define MERAM_SEC_LINE 15
  149. #define MERAM_LINE_WIDTH 2048
  150. /* -----------------------------------------------------------------------------
  151. * Registers access
  152. */
  153. #define MERAM_ICB_OFFSET(base, idx, off) ((base) + (off) + (idx) * 0x20)
  154. static inline void meram_write_icb(void __iomem *base, unsigned int idx,
  155. unsigned int off, unsigned long val)
  156. {
  157. iowrite32(val, MERAM_ICB_OFFSET(base, idx, off));
  158. }
  159. static inline unsigned long meram_read_icb(void __iomem *base, unsigned int idx,
  160. unsigned int off)
  161. {
  162. return ioread32(MERAM_ICB_OFFSET(base, idx, off));
  163. }
  164. static inline void meram_write_reg(void __iomem *base, unsigned int off,
  165. unsigned long val)
  166. {
  167. iowrite32(val, base + off);
  168. }
  169. static inline unsigned long meram_read_reg(void __iomem *base, unsigned int off)
  170. {
  171. return ioread32(base + off);
  172. }
  173. /* -----------------------------------------------------------------------------
  174. * MERAM allocation and free
  175. */
  176. static unsigned long meram_alloc(struct sh_mobile_meram_priv *priv, size_t size)
  177. {
  178. return gen_pool_alloc(priv->pool, size);
  179. }
  180. static void meram_free(struct sh_mobile_meram_priv *priv, unsigned long mem,
  181. size_t size)
  182. {
  183. gen_pool_free(priv->pool, mem, size);
  184. }
  185. /* -----------------------------------------------------------------------------
  186. * LCDC cache planes allocation, init, cleanup and free
  187. */
  188. /* Allocate ICBs and MERAM for a plane. */
  189. static int meram_plane_alloc(struct sh_mobile_meram_priv *priv,
  190. struct sh_mobile_meram_fb_plane *plane,
  191. size_t size)
  192. {
  193. unsigned long mem;
  194. unsigned long idx;
  195. idx = find_first_zero_bit(&priv->used_icb, 28);
  196. if (idx == 28)
  197. return -ENOMEM;
  198. plane->cache = &priv->icbs[idx];
  199. idx = find_next_zero_bit(&priv->used_icb, 32, 28);
  200. if (idx == 32)
  201. return -ENOMEM;
  202. plane->marker = &priv->icbs[idx];
  203. mem = meram_alloc(priv, size * 1024);
  204. if (mem == 0)
  205. return -ENOMEM;
  206. __set_bit(plane->marker->index, &priv->used_icb);
  207. __set_bit(plane->cache->index, &priv->used_icb);
  208. plane->marker->offset = mem - priv->meram;
  209. plane->marker->size = size;
  210. return 0;
  211. }
  212. /* Free ICBs and MERAM for a plane. */
  213. static void meram_plane_free(struct sh_mobile_meram_priv *priv,
  214. struct sh_mobile_meram_fb_plane *plane)
  215. {
  216. meram_free(priv, priv->meram + plane->marker->offset,
  217. plane->marker->size * 1024);
  218. __clear_bit(plane->marker->index, &priv->used_icb);
  219. __clear_bit(plane->cache->index, &priv->used_icb);
  220. }
  221. /* Is this a YCbCr(NV12, NV16 or NV24) colorspace? */
  222. static int is_nvcolor(int cspace)
  223. {
  224. if (cspace == SH_MOBILE_MERAM_PF_NV ||
  225. cspace == SH_MOBILE_MERAM_PF_NV24)
  226. return 1;
  227. return 0;
  228. }
  229. /* Set the next address to fetch. */
  230. static void meram_set_next_addr(struct sh_mobile_meram_priv *priv,
  231. struct sh_mobile_meram_fb_cache *cache,
  232. unsigned long base_addr_y,
  233. unsigned long base_addr_c)
  234. {
  235. struct sh_mobile_meram_icb *icb = cache->planes[0].marker;
  236. unsigned long target;
  237. icb->current_reg ^= 1;
  238. target = icb->current_reg ? MExxSARB : MExxSARA;
  239. /* set the next address to fetch */
  240. meram_write_icb(priv->base, cache->planes[0].cache->index, target,
  241. base_addr_y);
  242. meram_write_icb(priv->base, cache->planes[0].marker->index, target,
  243. base_addr_y + cache->planes[0].marker->cache_unit);
  244. if (cache->nplanes == 2) {
  245. meram_write_icb(priv->base, cache->planes[1].cache->index,
  246. target, base_addr_c);
  247. meram_write_icb(priv->base, cache->planes[1].marker->index,
  248. target, base_addr_c +
  249. cache->planes[1].marker->cache_unit);
  250. }
  251. }
  252. /* Get the next ICB address. */
  253. static void
  254. meram_get_next_icb_addr(struct sh_mobile_meram_info *pdata,
  255. struct sh_mobile_meram_fb_cache *cache,
  256. unsigned long *icb_addr_y, unsigned long *icb_addr_c)
  257. {
  258. struct sh_mobile_meram_icb *icb = cache->planes[0].marker;
  259. unsigned long icb_offset;
  260. if (pdata->addr_mode == SH_MOBILE_MERAM_MODE0)
  261. icb_offset = 0x80000000 | (icb->current_reg << 29);
  262. else
  263. icb_offset = 0xc0000000 | (icb->current_reg << 23);
  264. *icb_addr_y = icb_offset | (cache->planes[0].marker->index << 24);
  265. if (cache->nplanes == 2)
  266. *icb_addr_c = icb_offset
  267. | (cache->planes[1].marker->index << 24);
  268. }
  269. #define MERAM_CALC_BYTECOUNT(x, y) \
  270. (((x) * (y) + (MERAM_LINE_WIDTH - 1)) & ~(MERAM_LINE_WIDTH - 1))
  271. /* Initialize MERAM. */
  272. static int meram_plane_init(struct sh_mobile_meram_priv *priv,
  273. struct sh_mobile_meram_fb_plane *plane,
  274. unsigned int xres, unsigned int yres,
  275. unsigned int *out_pitch)
  276. {
  277. struct sh_mobile_meram_icb *marker = plane->marker;
  278. unsigned long total_byte_count = MERAM_CALC_BYTECOUNT(xres, yres);
  279. unsigned long bnm;
  280. unsigned int lcdc_pitch;
  281. unsigned int xpitch;
  282. unsigned int line_cnt;
  283. unsigned int save_lines;
  284. /* adjust pitch to 1024, 2048, 4096 or 8192 */
  285. lcdc_pitch = (xres - 1) | 1023;
  286. lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 1);
  287. lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 2);
  288. lcdc_pitch += 1;
  289. /* derive settings */
  290. if (lcdc_pitch == 8192 && yres >= 1024) {
  291. lcdc_pitch = xpitch = MERAM_LINE_WIDTH;
  292. line_cnt = total_byte_count >> 11;
  293. *out_pitch = xres;
  294. save_lines = plane->marker->size / 16 / MERAM_SEC_LINE;
  295. save_lines *= MERAM_SEC_LINE;
  296. } else {
  297. xpitch = xres;
  298. line_cnt = yres;
  299. *out_pitch = lcdc_pitch;
  300. save_lines = plane->marker->size / (lcdc_pitch >> 10) / 2;
  301. save_lines &= 0xff;
  302. }
  303. bnm = (save_lines - 1) << 16;
  304. /* TODO: we better to check if we have enough MERAM buffer size */
  305. /* set up ICB */
  306. meram_write_icb(priv->base, plane->cache->index, MExxBSIZE,
  307. MERAM_MExxBSIZE_VAL(0x0, line_cnt - 1, xpitch - 1));
  308. meram_write_icb(priv->base, plane->marker->index, MExxBSIZE,
  309. MERAM_MExxBSIZE_VAL(0xf, line_cnt - 1, xpitch - 1));
  310. meram_write_icb(priv->base, plane->cache->index, MExxMNCF, bnm);
  311. meram_write_icb(priv->base, plane->marker->index, MExxMNCF, bnm);
  312. meram_write_icb(priv->base, plane->cache->index, MExxSBSIZE, xpitch);
  313. meram_write_icb(priv->base, plane->marker->index, MExxSBSIZE, xpitch);
  314. /* save a cache unit size */
  315. plane->cache->cache_unit = xres * save_lines;
  316. plane->marker->cache_unit = xres * save_lines;
  317. /*
  318. * Set MERAM for framebuffer
  319. *
  320. * we also chain the cache_icb and the marker_icb.
  321. * we also split the allocated MERAM buffer between two ICBs.
  322. */
  323. meram_write_icb(priv->base, plane->cache->index, MExxCTL,
  324. MERAM_MExxCTL_VAL(plane->marker->index, marker->offset)
  325. | MExxCTL_WD1 | MExxCTL_WD0 | MExxCTL_WS | MExxCTL_CM |
  326. MExxCTL_MD_FB);
  327. meram_write_icb(priv->base, plane->marker->index, MExxCTL,
  328. MERAM_MExxCTL_VAL(plane->cache->index, marker->offset +
  329. plane->marker->size / 2) |
  330. MExxCTL_WD1 | MExxCTL_WD0 | MExxCTL_WS | MExxCTL_CM |
  331. MExxCTL_MD_FB);
  332. return 0;
  333. }
  334. static void meram_plane_cleanup(struct sh_mobile_meram_priv *priv,
  335. struct sh_mobile_meram_fb_plane *plane)
  336. {
  337. /* disable ICB */
  338. meram_write_icb(priv->base, plane->cache->index, MExxCTL,
  339. MExxCTL_WBF | MExxCTL_WF | MExxCTL_RF);
  340. meram_write_icb(priv->base, plane->marker->index, MExxCTL,
  341. MExxCTL_WBF | MExxCTL_WF | MExxCTL_RF);
  342. plane->cache->cache_unit = 0;
  343. plane->marker->cache_unit = 0;
  344. }
  345. /* -----------------------------------------------------------------------------
  346. * MERAM operations
  347. */
  348. unsigned long sh_mobile_meram_alloc(struct sh_mobile_meram_info *pdata,
  349. size_t size)
  350. {
  351. struct sh_mobile_meram_priv *priv = pdata->priv;
  352. return meram_alloc(priv, size);
  353. }
  354. EXPORT_SYMBOL_GPL(sh_mobile_meram_alloc);
  355. void sh_mobile_meram_free(struct sh_mobile_meram_info *pdata, unsigned long mem,
  356. size_t size)
  357. {
  358. struct sh_mobile_meram_priv *priv = pdata->priv;
  359. meram_free(priv, mem, size);
  360. }
  361. EXPORT_SYMBOL_GPL(sh_mobile_meram_free);
  362. /* Allocate memory for the ICBs and mark them as used. */
  363. static struct sh_mobile_meram_fb_cache *
  364. meram_cache_alloc(struct sh_mobile_meram_priv *priv,
  365. const struct sh_mobile_meram_cfg *cfg,
  366. int pixelformat)
  367. {
  368. unsigned int nplanes = is_nvcolor(pixelformat) ? 2 : 1;
  369. struct sh_mobile_meram_fb_cache *cache;
  370. int ret;
  371. cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  372. if (cache == NULL)
  373. return ERR_PTR(-ENOMEM);
  374. cache->nplanes = nplanes;
  375. ret = meram_plane_alloc(priv, &cache->planes[0],
  376. cfg->icb[0].meram_size);
  377. if (ret < 0)
  378. goto error;
  379. cache->planes[0].marker->current_reg = 1;
  380. cache->planes[0].marker->pixelformat = pixelformat;
  381. if (cache->nplanes == 1)
  382. return cache;
  383. ret = meram_plane_alloc(priv, &cache->planes[1],
  384. cfg->icb[1].meram_size);
  385. if (ret < 0) {
  386. meram_plane_free(priv, &cache->planes[0]);
  387. goto error;
  388. }
  389. return cache;
  390. error:
  391. kfree(cache);
  392. return ERR_PTR(-ENOMEM);
  393. }
  394. void *sh_mobile_meram_cache_alloc(struct sh_mobile_meram_info *pdata,
  395. const struct sh_mobile_meram_cfg *cfg,
  396. unsigned int xres, unsigned int yres,
  397. unsigned int pixelformat, unsigned int *pitch)
  398. {
  399. struct sh_mobile_meram_fb_cache *cache;
  400. struct sh_mobile_meram_priv *priv = pdata->priv;
  401. struct platform_device *pdev = pdata->pdev;
  402. unsigned int nplanes = is_nvcolor(pixelformat) ? 2 : 1;
  403. unsigned int out_pitch;
  404. if (priv == NULL)
  405. return ERR_PTR(-ENODEV);
  406. if (pixelformat != SH_MOBILE_MERAM_PF_NV &&
  407. pixelformat != SH_MOBILE_MERAM_PF_NV24 &&
  408. pixelformat != SH_MOBILE_MERAM_PF_RGB)
  409. return ERR_PTR(-EINVAL);
  410. dev_dbg(&pdev->dev, "registering %dx%d (%s)", xres, yres,
  411. !pixelformat ? "yuv" : "rgb");
  412. /* we can't handle wider than 8192px */
  413. if (xres > 8192) {
  414. dev_err(&pdev->dev, "width exceeding the limit (> 8192).");
  415. return ERR_PTR(-EINVAL);
  416. }
  417. if (cfg->icb[0].meram_size == 0)
  418. return ERR_PTR(-EINVAL);
  419. if (nplanes == 2 && cfg->icb[1].meram_size == 0)
  420. return ERR_PTR(-EINVAL);
  421. mutex_lock(&priv->lock);
  422. /* We now register the ICBs and allocate the MERAM regions. */
  423. cache = meram_cache_alloc(priv, cfg, pixelformat);
  424. if (IS_ERR(cache)) {
  425. dev_err(&pdev->dev, "MERAM allocation failed (%ld).",
  426. PTR_ERR(cache));
  427. goto err;
  428. }
  429. /* initialize MERAM */
  430. meram_plane_init(priv, &cache->planes[0], xres, yres, &out_pitch);
  431. *pitch = out_pitch;
  432. if (pixelformat == SH_MOBILE_MERAM_PF_NV)
  433. meram_plane_init(priv, &cache->planes[1],
  434. xres, (yres + 1) / 2, &out_pitch);
  435. else if (pixelformat == SH_MOBILE_MERAM_PF_NV24)
  436. meram_plane_init(priv, &cache->planes[1],
  437. 2 * xres, (yres + 1) / 2, &out_pitch);
  438. err:
  439. mutex_unlock(&priv->lock);
  440. return cache;
  441. }
  442. EXPORT_SYMBOL_GPL(sh_mobile_meram_cache_alloc);
  443. void
  444. sh_mobile_meram_cache_free(struct sh_mobile_meram_info *pdata, void *data)
  445. {
  446. struct sh_mobile_meram_fb_cache *cache = data;
  447. struct sh_mobile_meram_priv *priv = pdata->priv;
  448. mutex_lock(&priv->lock);
  449. /* Cleanup and free. */
  450. meram_plane_cleanup(priv, &cache->planes[0]);
  451. meram_plane_free(priv, &cache->planes[0]);
  452. if (cache->nplanes == 2) {
  453. meram_plane_cleanup(priv, &cache->planes[1]);
  454. meram_plane_free(priv, &cache->planes[1]);
  455. }
  456. kfree(cache);
  457. mutex_unlock(&priv->lock);
  458. }
  459. EXPORT_SYMBOL_GPL(sh_mobile_meram_cache_free);
  460. void
  461. sh_mobile_meram_cache_update(struct sh_mobile_meram_info *pdata, void *data,
  462. unsigned long base_addr_y,
  463. unsigned long base_addr_c,
  464. unsigned long *icb_addr_y,
  465. unsigned long *icb_addr_c)
  466. {
  467. struct sh_mobile_meram_fb_cache *cache = data;
  468. struct sh_mobile_meram_priv *priv = pdata->priv;
  469. mutex_lock(&priv->lock);
  470. meram_set_next_addr(priv, cache, base_addr_y, base_addr_c);
  471. meram_get_next_icb_addr(pdata, cache, icb_addr_y, icb_addr_c);
  472. mutex_unlock(&priv->lock);
  473. }
  474. EXPORT_SYMBOL_GPL(sh_mobile_meram_cache_update);
  475. /* -----------------------------------------------------------------------------
  476. * Power management
  477. */
  478. #ifdef CONFIG_PM
  479. static int sh_mobile_meram_suspend(struct device *dev)
  480. {
  481. struct platform_device *pdev = to_platform_device(dev);
  482. struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev);
  483. unsigned int i, j;
  484. for (i = 0; i < MERAM_REGS_SIZE; i++)
  485. priv->regs[i] = meram_read_reg(priv->base, common_regs[i]);
  486. for (i = 0; i < 32; i++) {
  487. if (!test_bit(i, &priv->used_icb))
  488. continue;
  489. for (j = 0; j < ICB_REGS_SIZE; j++) {
  490. priv->icbs[i].regs[j] =
  491. meram_read_icb(priv->base, i, icb_regs[j]);
  492. /* Reset ICB on resume */
  493. if (icb_regs[j] == MExxCTL)
  494. priv->icbs[i].regs[j] |=
  495. MExxCTL_WBF | MExxCTL_WF | MExxCTL_RF;
  496. }
  497. }
  498. return 0;
  499. }
  500. static int sh_mobile_meram_resume(struct device *dev)
  501. {
  502. struct platform_device *pdev = to_platform_device(dev);
  503. struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev);
  504. unsigned int i, j;
  505. for (i = 0; i < 32; i++) {
  506. if (!test_bit(i, &priv->used_icb))
  507. continue;
  508. for (j = 0; j < ICB_REGS_SIZE; j++)
  509. meram_write_icb(priv->base, i, icb_regs[j],
  510. priv->icbs[i].regs[j]);
  511. }
  512. for (i = 0; i < MERAM_REGS_SIZE; i++)
  513. meram_write_reg(priv->base, common_regs[i], priv->regs[i]);
  514. return 0;
  515. }
  516. #endif /* CONFIG_PM */
  517. static UNIVERSAL_DEV_PM_OPS(sh_mobile_meram_dev_pm_ops,
  518. sh_mobile_meram_suspend,
  519. sh_mobile_meram_resume, NULL);
  520. /* -----------------------------------------------------------------------------
  521. * Probe/remove and driver init/exit
  522. */
  523. static int sh_mobile_meram_probe(struct platform_device *pdev)
  524. {
  525. struct sh_mobile_meram_priv *priv;
  526. struct sh_mobile_meram_info *pdata = pdev->dev.platform_data;
  527. struct resource *regs;
  528. struct resource *meram;
  529. unsigned int i;
  530. int error;
  531. if (!pdata) {
  532. dev_err(&pdev->dev, "no platform data defined\n");
  533. return -EINVAL;
  534. }
  535. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  536. meram = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  537. if (regs == NULL || meram == NULL) {
  538. dev_err(&pdev->dev, "cannot get platform resources\n");
  539. return -ENOENT;
  540. }
  541. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  542. if (!priv) {
  543. dev_err(&pdev->dev, "cannot allocate device data\n");
  544. return -ENOMEM;
  545. }
  546. /* Initialize private data. */
  547. mutex_init(&priv->lock);
  548. priv->used_icb = pdata->reserved_icbs;
  549. for (i = 0; i < MERAM_ICB_NUM; ++i)
  550. priv->icbs[i].index = i;
  551. pdata->priv = priv;
  552. pdata->pdev = pdev;
  553. /* Request memory regions and remap the registers. */
  554. if (!request_mem_region(regs->start, resource_size(regs), pdev->name)) {
  555. dev_err(&pdev->dev, "MERAM registers region already claimed\n");
  556. error = -EBUSY;
  557. goto err_req_regs;
  558. }
  559. if (!request_mem_region(meram->start, resource_size(meram),
  560. pdev->name)) {
  561. dev_err(&pdev->dev, "MERAM memory region already claimed\n");
  562. error = -EBUSY;
  563. goto err_req_meram;
  564. }
  565. priv->base = ioremap_nocache(regs->start, resource_size(regs));
  566. if (!priv->base) {
  567. dev_err(&pdev->dev, "ioremap failed\n");
  568. error = -EFAULT;
  569. goto err_ioremap;
  570. }
  571. priv->meram = meram->start;
  572. /* Create and initialize the MERAM memory pool. */
  573. priv->pool = gen_pool_create(ilog2(MERAM_GRANULARITY), -1);
  574. if (priv->pool == NULL) {
  575. error = -ENOMEM;
  576. goto err_genpool;
  577. }
  578. error = gen_pool_add(priv->pool, meram->start, resource_size(meram),
  579. -1);
  580. if (error < 0)
  581. goto err_genpool;
  582. /* initialize ICB addressing mode */
  583. if (pdata->addr_mode == SH_MOBILE_MERAM_MODE1)
  584. meram_write_reg(priv->base, MEVCR1, MEVCR1_AMD1);
  585. platform_set_drvdata(pdev, priv);
  586. pm_runtime_enable(&pdev->dev);
  587. dev_info(&pdev->dev, "sh_mobile_meram initialized.");
  588. return 0;
  589. err_genpool:
  590. if (priv->pool)
  591. gen_pool_destroy(priv->pool);
  592. iounmap(priv->base);
  593. err_ioremap:
  594. release_mem_region(meram->start, resource_size(meram));
  595. err_req_meram:
  596. release_mem_region(regs->start, resource_size(regs));
  597. err_req_regs:
  598. mutex_destroy(&priv->lock);
  599. kfree(priv);
  600. return error;
  601. }
  602. static int sh_mobile_meram_remove(struct platform_device *pdev)
  603. {
  604. struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev);
  605. struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  606. struct resource *meram = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  607. pm_runtime_disable(&pdev->dev);
  608. gen_pool_destroy(priv->pool);
  609. iounmap(priv->base);
  610. release_mem_region(meram->start, resource_size(meram));
  611. release_mem_region(regs->start, resource_size(regs));
  612. mutex_destroy(&priv->lock);
  613. kfree(priv);
  614. return 0;
  615. }
  616. static struct platform_driver sh_mobile_meram_driver = {
  617. .driver = {
  618. .name = "sh_mobile_meram",
  619. .pm = &sh_mobile_meram_dev_pm_ops,
  620. },
  621. .probe = sh_mobile_meram_probe,
  622. .remove = sh_mobile_meram_remove,
  623. };
  624. module_platform_driver(sh_mobile_meram_driver);
  625. MODULE_DESCRIPTION("SuperH Mobile MERAM driver");
  626. MODULE_AUTHOR("Damian Hobson-Garcia / Takanari Hayama");
  627. MODULE_LICENSE("GPL v2");