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