video: mxsfb: reorder includes
[oweals/u-boot.git] / drivers / video / mxsfb.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Freescale i.MX23/i.MX28 LCDIF driver
4  *
5  * Copyright (C) 2011-2013 Marek Vasut <marex@denx.de>
6  */
7 #include <common.h>
8 #include <linux/errno.h>
9 #include <malloc.h>
10 #include <video_fb.h>
11
12 #include <asm/arch/clock.h>
13 #include <asm/arch/imx-regs.h>
14 #include <asm/arch/sys_proto.h>
15 #include <asm/mach-imx/dma.h>
16 #include <asm/io.h>
17
18 #include "videomodes.h"
19
20 #define PS2KHZ(ps)      (1000000000UL / (ps))
21
22 static GraphicDevice panel;
23 struct mxs_dma_desc desc;
24
25 /**
26  * mxsfb_system_setup() - Fine-tune LCDIF configuration
27  *
28  * This function is used to adjust the LCDIF configuration. This is usually
29  * needed when driving the controller in System-Mode to operate an 8080 or
30  * 6800 connected SmartLCD.
31  */
32 __weak void mxsfb_system_setup(void)
33 {
34 }
35
36 /*
37  * ARIES M28EVK:
38  * setenv videomode
39  * video=ctfb:x:800,y:480,depth:18,mode:0,pclk:30066,
40  *       le:0,ri:256,up:0,lo:45,hs:1,vs:1,sync:100663296,vmode:0
41  *
42  * Freescale mx23evk/mx28evk with a Seiko 4.3'' WVGA panel:
43  * setenv videomode
44  * video=ctfb:x:800,y:480,depth:24,mode:0,pclk:29851,
45  *       le:89,ri:164,up:23,lo:10,hs:10,vs:10,sync:0,vmode:0
46  */
47
48 static void mxs_lcd_init(u32 fb_addr, struct ctfb_res_modes *mode, int bpp)
49 {
50         struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
51         uint32_t word_len = 0, bus_width = 0;
52         uint8_t valid_data = 0;
53
54         /* Kick in the LCDIF clock */
55         mxs_set_lcdclk(MXS_LCDIF_BASE, PS2KHZ(mode->pixclock));
56
57         /* Restart the LCDIF block */
58         mxs_reset_block(&regs->hw_lcdif_ctrl_reg);
59
60         switch (bpp) {
61         case 24:
62                 word_len = LCDIF_CTRL_WORD_LENGTH_24BIT;
63                 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_24BIT;
64                 valid_data = 0x7;
65                 break;
66         case 18:
67                 word_len = LCDIF_CTRL_WORD_LENGTH_24BIT;
68                 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_18BIT;
69                 valid_data = 0x7;
70                 break;
71         case 16:
72                 word_len = LCDIF_CTRL_WORD_LENGTH_16BIT;
73                 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_16BIT;
74                 valid_data = 0xf;
75                 break;
76         case 8:
77                 word_len = LCDIF_CTRL_WORD_LENGTH_8BIT;
78                 bus_width = LCDIF_CTRL_LCD_DATABUS_WIDTH_8BIT;
79                 valid_data = 0xf;
80                 break;
81         }
82
83         writel(bus_width | word_len | LCDIF_CTRL_DOTCLK_MODE |
84                 LCDIF_CTRL_BYPASS_COUNT | LCDIF_CTRL_LCDIF_MASTER,
85                 &regs->hw_lcdif_ctrl);
86
87         writel(valid_data << LCDIF_CTRL1_BYTE_PACKING_FORMAT_OFFSET,
88                 &regs->hw_lcdif_ctrl1);
89
90         mxsfb_system_setup();
91
92         writel((mode->yres << LCDIF_TRANSFER_COUNT_V_COUNT_OFFSET) | mode->xres,
93                 &regs->hw_lcdif_transfer_count);
94
95         writel(LCDIF_VDCTRL0_ENABLE_PRESENT | LCDIF_VDCTRL0_ENABLE_POL |
96                 LCDIF_VDCTRL0_VSYNC_PERIOD_UNIT |
97                 LCDIF_VDCTRL0_VSYNC_PULSE_WIDTH_UNIT |
98                 mode->vsync_len, &regs->hw_lcdif_vdctrl0);
99         writel(mode->upper_margin + mode->lower_margin +
100                 mode->vsync_len + mode->yres,
101                 &regs->hw_lcdif_vdctrl1);
102         writel((mode->hsync_len << LCDIF_VDCTRL2_HSYNC_PULSE_WIDTH_OFFSET) |
103                 (mode->left_margin + mode->right_margin +
104                 mode->hsync_len + mode->xres),
105                 &regs->hw_lcdif_vdctrl2);
106         writel(((mode->left_margin + mode->hsync_len) <<
107                 LCDIF_VDCTRL3_HORIZONTAL_WAIT_CNT_OFFSET) |
108                 (mode->upper_margin + mode->vsync_len),
109                 &regs->hw_lcdif_vdctrl3);
110         writel((0 << LCDIF_VDCTRL4_DOTCLK_DLY_SEL_OFFSET) | mode->xres,
111                 &regs->hw_lcdif_vdctrl4);
112
113         writel(fb_addr, &regs->hw_lcdif_cur_buf);
114         writel(fb_addr, &regs->hw_lcdif_next_buf);
115
116         /* Flush FIFO first */
117         writel(LCDIF_CTRL1_FIFO_CLEAR, &regs->hw_lcdif_ctrl1_set);
118
119 #ifndef CONFIG_VIDEO_MXS_MODE_SYSTEM
120         /* Sync signals ON */
121         setbits_le32(&regs->hw_lcdif_vdctrl4, LCDIF_VDCTRL4_SYNC_SIGNALS_ON);
122 #endif
123
124         /* FIFO cleared */
125         writel(LCDIF_CTRL1_FIFO_CLEAR, &regs->hw_lcdif_ctrl1_clr);
126
127         /* RUN! */
128         writel(LCDIF_CTRL_RUN, &regs->hw_lcdif_ctrl_set);
129 }
130
131 void lcdif_power_down(void)
132 {
133         struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
134         int timeout = 1000000;
135
136         if (!panel.frameAdrs)
137                 return;
138
139         writel(panel.frameAdrs, &regs->hw_lcdif_cur_buf_reg);
140         writel(panel.frameAdrs, &regs->hw_lcdif_next_buf_reg);
141         writel(LCDIF_CTRL1_VSYNC_EDGE_IRQ, &regs->hw_lcdif_ctrl1_clr);
142         while (--timeout) {
143                 if (readl(&regs->hw_lcdif_ctrl1_reg) &
144                     LCDIF_CTRL1_VSYNC_EDGE_IRQ)
145                         break;
146                 udelay(1);
147         }
148         mxs_reset_block((struct mxs_register_32 *)&regs->hw_lcdif_ctrl_reg);
149 }
150
151 void *video_hw_init(void)
152 {
153         int bpp = -1;
154         char *penv;
155         void *fb;
156         struct ctfb_res_modes mode;
157
158         puts("Video: ");
159
160         /* Suck display configuration from "videomode" variable */
161         penv = env_get("videomode");
162         if (!penv) {
163                 puts("MXSFB: 'videomode' variable not set!\n");
164                 return NULL;
165         }
166
167         bpp = video_get_params(&mode, penv);
168
169         /* fill in Graphic device struct */
170         sprintf(panel.modeIdent, "%dx%dx%d",
171                         mode.xres, mode.yres, bpp);
172
173         panel.winSizeX = mode.xres;
174         panel.winSizeY = mode.yres;
175         panel.plnSizeX = mode.xres;
176         panel.plnSizeY = mode.yres;
177
178         switch (bpp) {
179         case 24:
180         case 18:
181                 panel.gdfBytesPP = 4;
182                 panel.gdfIndex = GDF_32BIT_X888RGB;
183                 break;
184         case 16:
185                 panel.gdfBytesPP = 2;
186                 panel.gdfIndex = GDF_16BIT_565RGB;
187                 break;
188         case 8:
189                 panel.gdfBytesPP = 1;
190                 panel.gdfIndex = GDF__8BIT_INDEX;
191                 break;
192         default:
193                 printf("MXSFB: Invalid BPP specified! (bpp = %i)\n", bpp);
194                 return NULL;
195         }
196
197         panel.memSize = mode.xres * mode.yres * panel.gdfBytesPP;
198
199         /* Allocate framebuffer */
200         fb = memalign(ARCH_DMA_MINALIGN,
201                       roundup(panel.memSize, ARCH_DMA_MINALIGN));
202         if (!fb) {
203                 printf("MXSFB: Error allocating framebuffer!\n");
204                 return NULL;
205         }
206
207         /* Wipe framebuffer */
208         memset(fb, 0, panel.memSize);
209
210         panel.frameAdrs = (u32)fb;
211
212         printf("%s\n", panel.modeIdent);
213
214         /* Start framebuffer */
215         mxs_lcd_init(panel.frameAdrs, &mode, bpp);
216
217 #ifdef CONFIG_VIDEO_MXS_MODE_SYSTEM
218         /*
219          * If the LCD runs in system mode, the LCD refresh has to be triggered
220          * manually by setting the RUN bit in HW_LCDIF_CTRL register. To avoid
221          * having to set this bit manually after every single change in the
222          * framebuffer memory, we set up specially crafted circular DMA, which
223          * sets the RUN bit, then waits until it gets cleared and repeats this
224          * infinitelly. This way, we get smooth continuous updates of the LCD.
225          */
226         struct mxs_lcdif_regs *regs = (struct mxs_lcdif_regs *)MXS_LCDIF_BASE;
227
228         memset(&desc, 0, sizeof(struct mxs_dma_desc));
229         desc.address = (dma_addr_t)&desc;
230         desc.cmd.data = MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
231                         MXS_DMA_DESC_WAIT4END |
232                         (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
233         desc.cmd.pio_words[0] = readl(&regs->hw_lcdif_ctrl) | LCDIF_CTRL_RUN;
234         desc.cmd.next = (uint32_t)&desc.cmd;
235
236         /* Execute the DMA chain. */
237         mxs_dma_circ_start(MXS_DMA_CHANNEL_AHB_APBH_LCDIF, &desc);
238 #endif
239
240         return (void *)&panel;
241 }