1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/aperture.h>
3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/errno.h>
6 #include <linux/string.h>
7 #include <linux/mm.h>
8 #include <linux/slab.h>
9 #include <linux/delay.h>
10 #include <linux/fb.h>
11 #include <linux/ioport.h>
12 #include <linux/init.h>
13 #include <linux/pci.h>
14 #include <linux/mm_types.h>
15 #include <linux/vmalloc.h>
16 #include <linux/pagemap.h>
17 #include <linux/screen_info.h>
18 #include <linux/console.h>
19 #include <asm/fb.h>
20 #include "sm750.h"
21 #include "sm750_accel.h"
22 #include "sm750_cursor.h"
23
24 /*
25 * #ifdef __BIG_ENDIAN
26 * ssize_t lynxfb_ops_write(struct fb_info *info, const char __user *buf,
27 * size_t count, loff_t *ppos);
28 * ssize_t lynxfb_ops_read(struct fb_info *info, char __user *buf,
29 * size_t count, loff_t *ppos);
30 * #endif
31 */
32
33 /* common var for all device */
34 static int g_hwcursor = 1;
35 static int g_noaccel;
36 static int g_nomtrr;
37 static const char *g_fbmode[] = {NULL, NULL};
38 static const char *g_def_fbmode = "1024x768-32@60";
39 static char *g_settings;
40 static int g_dualview;
41 static char *g_option;
42
43 static const struct fb_videomode lynx750_ext[] = {
44 /* 1024x600-60 VESA [1.71:1] */
45 {NULL, 60, 1024, 600, 20423, 144, 40, 18, 1, 104, 3,
46 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
47 FB_VMODE_NONINTERLACED},
48
49 /* 1024x600-70 VESA */
50 {NULL, 70, 1024, 600, 17211, 152, 48, 21, 1, 104, 3,
51 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
52 FB_VMODE_NONINTERLACED},
53
54 /* 1024x600-75 VESA */
55 {NULL, 75, 1024, 600, 15822, 160, 56, 23, 1, 104, 3,
56 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
57 FB_VMODE_NONINTERLACED},
58
59 /* 1024x600-85 VESA */
60 {NULL, 85, 1024, 600, 13730, 168, 56, 26, 1, 112, 3,
61 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
62 FB_VMODE_NONINTERLACED},
63
64 /* 720x480 */
65 {NULL, 60, 720, 480, 37427, 88, 16, 13, 1, 72, 3,
66 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
67 FB_VMODE_NONINTERLACED},
68
69 /* 1280x720 [1.78:1] */
70 {NULL, 60, 1280, 720, 13426, 162, 86, 22, 1, 136, 3,
71 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
72 FB_VMODE_NONINTERLACED},
73
74 /* 1280x768@60 */
75 {NULL, 60, 1280, 768, 12579, 192, 64, 20, 3, 128, 7,
76 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
77 FB_VMODE_NONINTERLACED},
78
79 /* 1360 x 768 [1.77083:1] */
80 {NULL, 60, 1360, 768, 11804, 208, 64, 23, 1, 144, 3,
81 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
82 FB_VMODE_NONINTERLACED},
83
84 /* 1368 x 768 [1.78:1] */
85 {NULL, 60, 1368, 768, 11647, 216, 72, 23, 1, 144, 3,
86 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
87 FB_VMODE_NONINTERLACED},
88
89 /* 1440 x 900 [16:10] */
90 {NULL, 60, 1440, 900, 9392, 232, 80, 28, 1, 152, 3,
91 FB_SYNC_VERT_HIGH_ACT,
92 FB_VMODE_NONINTERLACED},
93
94 /* 1440x960 [15:10] */
95 {NULL, 60, 1440, 960, 8733, 240, 88, 30, 1, 152, 3,
96 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
97 FB_VMODE_NONINTERLACED},
98
99 /* 1920x1080 [16:9] */
100 {NULL, 60, 1920, 1080, 6734, 148, 88, 41, 1, 44, 3,
101 FB_SYNC_VERT_HIGH_ACT,
102 FB_VMODE_NONINTERLACED},
103 };
104
105 /* no hardware cursor supported under version 2.6.10, kernel bug */
lynxfb_ops_cursor(struct fb_info * info,struct fb_cursor * fbcursor)106 static int lynxfb_ops_cursor(struct fb_info *info, struct fb_cursor *fbcursor)
107 {
108 struct lynxfb_par *par;
109 struct lynxfb_crtc *crtc;
110 struct lynx_cursor *cursor;
111
112 par = info->par;
113 crtc = &par->crtc;
114 cursor = &crtc->cursor;
115
116 if (fbcursor->image.width > cursor->max_w ||
117 fbcursor->image.height > cursor->max_h ||
118 fbcursor->image.depth > 1) {
119 return -ENXIO;
120 }
121
122 sm750_hw_cursor_disable(cursor);
123 if (fbcursor->set & FB_CUR_SETSIZE)
124 sm750_hw_cursor_setSize(cursor,
125 fbcursor->image.width,
126 fbcursor->image.height);
127
128 if (fbcursor->set & FB_CUR_SETPOS)
129 sm750_hw_cursor_setPos(cursor,
130 fbcursor->image.dx - info->var.xoffset,
131 fbcursor->image.dy - info->var.yoffset);
132
133 if (fbcursor->set & FB_CUR_SETCMAP) {
134 /* get the 16bit color of kernel means */
135 u16 fg, bg;
136
137 fg = ((info->cmap.red[fbcursor->image.fg_color] & 0xf800)) |
138 ((info->cmap.green[fbcursor->image.fg_color] & 0xfc00) >> 5) |
139 ((info->cmap.blue[fbcursor->image.fg_color] & 0xf800) >> 11);
140
141 bg = ((info->cmap.red[fbcursor->image.bg_color] & 0xf800)) |
142 ((info->cmap.green[fbcursor->image.bg_color] & 0xfc00) >> 5) |
143 ((info->cmap.blue[fbcursor->image.bg_color] & 0xf800) >> 11);
144
145 sm750_hw_cursor_setColor(cursor, fg, bg);
146 }
147
148 if (fbcursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETIMAGE)) {
149 sm750_hw_cursor_setData(cursor,
150 fbcursor->rop,
151 fbcursor->image.data,
152 fbcursor->mask);
153 }
154
155 if (fbcursor->enable)
156 sm750_hw_cursor_enable(cursor);
157
158 return 0;
159 }
160
lynxfb_ops_fillrect(struct fb_info * info,const struct fb_fillrect * region)161 static void lynxfb_ops_fillrect(struct fb_info *info,
162 const struct fb_fillrect *region)
163 {
164 struct lynxfb_par *par;
165 struct sm750_dev *sm750_dev;
166 unsigned int base, pitch, Bpp, rop;
167 u32 color;
168
169 if (info->state != FBINFO_STATE_RUNNING)
170 return;
171
172 par = info->par;
173 sm750_dev = par->dev;
174
175 /*
176 * each time 2d function begin to work,below three variable always need
177 * be set, seems we can put them together in some place
178 */
179 base = par->crtc.o_screen;
180 pitch = info->fix.line_length;
181 Bpp = info->var.bits_per_pixel >> 3;
182
183 color = (Bpp == 1) ? region->color :
184 ((u32 *)info->pseudo_palette)[region->color];
185 rop = (region->rop != ROP_COPY) ? HW_ROP2_XOR : HW_ROP2_COPY;
186
187 /*
188 * If not use spin_lock, system will die if user load driver
189 * and immediately unload driver frequently (dual)
190 * since they fb_count could change during the lifetime of
191 * this lock, we are holding it for all cases.
192 */
193 spin_lock(&sm750_dev->slock);
194
195 sm750_dev->accel.de_fillrect(&sm750_dev->accel,
196 base, pitch, Bpp,
197 region->dx, region->dy,
198 region->width, region->height,
199 color, rop);
200 spin_unlock(&sm750_dev->slock);
201 }
202
lynxfb_ops_copyarea(struct fb_info * info,const struct fb_copyarea * region)203 static void lynxfb_ops_copyarea(struct fb_info *info,
204 const struct fb_copyarea *region)
205 {
206 struct lynxfb_par *par;
207 struct sm750_dev *sm750_dev;
208 unsigned int base, pitch, Bpp;
209
210 par = info->par;
211 sm750_dev = par->dev;
212
213 /*
214 * each time 2d function begin to work,below three variable always need
215 * be set, seems we can put them together in some place
216 */
217 base = par->crtc.o_screen;
218 pitch = info->fix.line_length;
219 Bpp = info->var.bits_per_pixel >> 3;
220
221 /*
222 * If not use spin_lock, system will die if user load driver
223 * and immediately unload driver frequently (dual)
224 * since they fb_count could change during the lifetime of
225 * this lock, we are holding it for all cases.
226 */
227 spin_lock(&sm750_dev->slock);
228
229 sm750_dev->accel.de_copyarea(&sm750_dev->accel,
230 base, pitch, region->sx, region->sy,
231 base, pitch, Bpp, region->dx, region->dy,
232 region->width, region->height,
233 HW_ROP2_COPY);
234 spin_unlock(&sm750_dev->slock);
235 }
236
lynxfb_ops_imageblit(struct fb_info * info,const struct fb_image * image)237 static void lynxfb_ops_imageblit(struct fb_info *info,
238 const struct fb_image *image)
239 {
240 unsigned int base, pitch, Bpp;
241 unsigned int fgcol, bgcol;
242 struct lynxfb_par *par;
243 struct sm750_dev *sm750_dev;
244
245 par = info->par;
246 sm750_dev = par->dev;
247 /*
248 * each time 2d function begin to work,below three variable always need
249 * be set, seems we can put them together in some place
250 */
251 base = par->crtc.o_screen;
252 pitch = info->fix.line_length;
253 Bpp = info->var.bits_per_pixel >> 3;
254
255 /* TODO: Implement hardware acceleration for image->depth > 1 */
256 if (image->depth != 1) {
257 cfb_imageblit(info, image);
258 return;
259 }
260
261 if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
262 info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
263 fgcol = ((u32 *)info->pseudo_palette)[image->fg_color];
264 bgcol = ((u32 *)info->pseudo_palette)[image->bg_color];
265 } else {
266 fgcol = image->fg_color;
267 bgcol = image->bg_color;
268 }
269
270 /*
271 * If not use spin_lock, system will die if user load driver
272 * and immediately unload driver frequently (dual)
273 * since they fb_count could change during the lifetime of
274 * this lock, we are holding it for all cases.
275 */
276 spin_lock(&sm750_dev->slock);
277
278 sm750_dev->accel.de_imageblit(&sm750_dev->accel,
279 image->data, image->width >> 3, 0,
280 base, pitch, Bpp,
281 image->dx, image->dy,
282 image->width, image->height,
283 fgcol, bgcol, HW_ROP2_COPY);
284 spin_unlock(&sm750_dev->slock);
285 }
286
lynxfb_ops_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)287 static int lynxfb_ops_pan_display(struct fb_var_screeninfo *var,
288 struct fb_info *info)
289 {
290 struct lynxfb_par *par;
291 struct lynxfb_crtc *crtc;
292
293 if (!info)
294 return -EINVAL;
295
296 par = info->par;
297 crtc = &par->crtc;
298 return hw_sm750_pan_display(crtc, var, info);
299 }
300
lynxfb_set_visual_mode(struct fb_info * info)301 static inline void lynxfb_set_visual_mode(struct fb_info *info)
302 {
303 switch (info->var.bits_per_pixel) {
304 case 8:
305 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
306 break;
307 case 16:
308 case 24:
309 case 32:
310 info->fix.visual = FB_VISUAL_TRUECOLOR;
311 break;
312 default:
313 break;
314 }
315 }
316
lynxfb_set_color_offsets(struct fb_info * info)317 static inline int lynxfb_set_color_offsets(struct fb_info *info)
318 {
319 lynxfb_set_visual_mode(info);
320
321 switch (info->var.bits_per_pixel) {
322 case 8:
323 info->var.red.offset = 0;
324 info->var.red.length = 8;
325 info->var.green.offset = 0;
326 info->var.green.length = 8;
327 info->var.blue.offset = 0;
328 info->var.blue.length = 8;
329 info->var.transp.length = 0;
330 info->var.transp.offset = 0;
331 break;
332 case 16:
333 info->var.red.offset = 11;
334 info->var.red.length = 5;
335 info->var.green.offset = 5;
336 info->var.green.length = 6;
337 info->var.blue.offset = 0;
338 info->var.blue.length = 5;
339 info->var.transp.length = 0;
340 info->var.transp.offset = 0;
341 break;
342 case 24:
343 case 32:
344 info->var.red.offset = 16;
345 info->var.red.length = 8;
346 info->var.green.offset = 8;
347 info->var.green.length = 8;
348 info->var.blue.offset = 0;
349 info->var.blue.length = 8;
350 break;
351 default:
352 return -EINVAL;
353 }
354 return 0;
355 }
356
lynxfb_ops_set_par(struct fb_info * info)357 static int lynxfb_ops_set_par(struct fb_info *info)
358 {
359 struct lynxfb_par *par;
360 struct lynxfb_crtc *crtc;
361 struct lynxfb_output *output;
362 struct fb_var_screeninfo *var;
363 struct fb_fix_screeninfo *fix;
364 int ret;
365 unsigned int line_length;
366
367 if (!info)
368 return -EINVAL;
369
370 ret = 0;
371 par = info->par;
372 crtc = &par->crtc;
373 output = &par->output;
374 var = &info->var;
375 fix = &info->fix;
376
377 /* fix structure is not so FIX ... */
378 line_length = var->xres_virtual * var->bits_per_pixel / 8;
379 line_length = ALIGN(line_length, crtc->line_pad);
380 fix->line_length = line_length;
381 pr_info("fix->line_length = %d\n", fix->line_length);
382
383 /*
384 * var->red,green,blue,transp are need to be set by driver
385 * and these data should be set before setcolreg routine
386 */
387
388 ret = lynxfb_set_color_offsets(info);
389
390 var->height = -1;
391 var->width = -1;
392 var->accel_flags = 0;/*FB_ACCELF_TEXT;*/
393
394 if (ret) {
395 pr_err("bpp %d not supported\n", var->bits_per_pixel);
396 return ret;
397 }
398 ret = hw_sm750_crtc_setMode(crtc, var, fix);
399 if (!ret)
400 ret = hw_sm750_output_setMode(output, var, fix);
401 return ret;
402 }
403
chan_to_field(unsigned int chan,struct fb_bitfield * bf)404 static inline unsigned int chan_to_field(unsigned int chan,
405 struct fb_bitfield *bf)
406 {
407 chan &= 0xffff;
408 chan >>= 16 - bf->length;
409 return chan << bf->offset;
410 }
411
lynxfb_suspend(struct device * dev)412 static int __maybe_unused lynxfb_suspend(struct device *dev)
413 {
414 struct fb_info *info;
415 struct sm750_dev *sm750_dev;
416
417 sm750_dev = dev_get_drvdata(dev);
418
419 console_lock();
420 info = sm750_dev->fbinfo[0];
421 if (info)
422 /* 1 means do suspend */
423 fb_set_suspend(info, 1);
424 info = sm750_dev->fbinfo[1];
425 if (info)
426 /* 1 means do suspend */
427 fb_set_suspend(info, 1);
428
429 console_unlock();
430 return 0;
431 }
432
lynxfb_resume(struct device * dev)433 static int __maybe_unused lynxfb_resume(struct device *dev)
434 {
435 struct pci_dev *pdev = to_pci_dev(dev);
436 struct fb_info *info;
437 struct sm750_dev *sm750_dev;
438
439 struct lynxfb_par *par;
440 struct lynxfb_crtc *crtc;
441 struct lynx_cursor *cursor;
442
443 sm750_dev = pci_get_drvdata(pdev);
444
445 console_lock();
446
447 hw_sm750_inithw(sm750_dev, pdev);
448
449 info = sm750_dev->fbinfo[0];
450
451 if (info) {
452 par = info->par;
453 crtc = &par->crtc;
454 cursor = &crtc->cursor;
455 memset_io(cursor->vstart, 0x0, cursor->size);
456 memset_io(crtc->v_screen, 0x0, crtc->vidmem_size);
457 lynxfb_ops_set_par(info);
458 fb_set_suspend(info, 0);
459 }
460
461 info = sm750_dev->fbinfo[1];
462
463 if (info) {
464 par = info->par;
465 crtc = &par->crtc;
466 cursor = &crtc->cursor;
467 memset_io(cursor->vstart, 0x0, cursor->size);
468 memset_io(crtc->v_screen, 0x0, crtc->vidmem_size);
469 lynxfb_ops_set_par(info);
470 fb_set_suspend(info, 0);
471 }
472
473 pdev->dev.power.power_state.event = PM_EVENT_RESUME;
474
475 console_unlock();
476 return 0;
477 }
478
lynxfb_ops_check_var(struct fb_var_screeninfo * var,struct fb_info * info)479 static int lynxfb_ops_check_var(struct fb_var_screeninfo *var,
480 struct fb_info *info)
481 {
482 int ret;
483 struct lynxfb_par *par;
484 struct lynxfb_crtc *crtc;
485 resource_size_t request;
486
487 ret = 0;
488 par = info->par;
489 crtc = &par->crtc;
490
491 pr_debug("check var:%dx%d-%d\n",
492 var->xres,
493 var->yres,
494 var->bits_per_pixel);
495
496 ret = lynxfb_set_color_offsets(info);
497
498 if (ret) {
499 pr_err("bpp %d not supported\n", var->bits_per_pixel);
500 return ret;
501 }
502
503 var->height = -1;
504 var->width = -1;
505 var->accel_flags = 0;/* FB_ACCELF_TEXT; */
506
507 /* check if current fb's video memory big enough to hold the onscreen*/
508 request = var->xres_virtual * (var->bits_per_pixel >> 3);
509 /* defaulty crtc->channel go with par->index */
510
511 request = ALIGN(request, crtc->line_pad);
512 request = request * var->yres_virtual;
513 if (crtc->vidmem_size < request) {
514 pr_err("not enough video memory for mode\n");
515 return -ENOMEM;
516 }
517
518 return hw_sm750_crtc_checkMode(crtc, var);
519 }
520
lynxfb_ops_setcolreg(unsigned int regno,unsigned int red,unsigned int green,unsigned int blue,unsigned int transp,struct fb_info * info)521 static int lynxfb_ops_setcolreg(unsigned int regno,
522 unsigned int red,
523 unsigned int green,
524 unsigned int blue,
525 unsigned int transp,
526 struct fb_info *info)
527 {
528 struct lynxfb_par *par;
529 struct lynxfb_crtc *crtc;
530 struct fb_var_screeninfo *var;
531 int ret;
532
533 par = info->par;
534 crtc = &par->crtc;
535 var = &info->var;
536 ret = 0;
537
538 if (regno > 256) {
539 pr_err("regno = %d\n", regno);
540 return -EINVAL;
541 }
542
543 if (info->var.grayscale)
544 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
545
546 if (var->bits_per_pixel == 8 &&
547 info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
548 red >>= 8;
549 green >>= 8;
550 blue >>= 8;
551 ret = hw_sm750_setColReg(crtc, regno, red, green, blue);
552 goto exit;
553 }
554
555 if (info->fix.visual == FB_VISUAL_TRUECOLOR && regno < 256) {
556 u32 val;
557
558 if (var->bits_per_pixel == 16 ||
559 var->bits_per_pixel == 32 ||
560 var->bits_per_pixel == 24) {
561 val = chan_to_field(red, &var->red);
562 val |= chan_to_field(green, &var->green);
563 val |= chan_to_field(blue, &var->blue);
564 par->pseudo_palette[regno] = val;
565 goto exit;
566 }
567 }
568
569 ret = -EINVAL;
570
571 exit:
572 return ret;
573 }
574
lynxfb_ops_blank(int blank,struct fb_info * info)575 static int lynxfb_ops_blank(int blank, struct fb_info *info)
576 {
577 struct lynxfb_par *par;
578 struct lynxfb_output *output;
579
580 pr_debug("blank = %d.\n", blank);
581 par = info->par;
582 output = &par->output;
583 return output->proc_setBLANK(output, blank);
584 }
585
sm750fb_set_drv(struct lynxfb_par * par)586 static int sm750fb_set_drv(struct lynxfb_par *par)
587 {
588 int ret;
589 struct sm750_dev *sm750_dev;
590 struct lynxfb_output *output;
591 struct lynxfb_crtc *crtc;
592
593 ret = 0;
594
595 sm750_dev = par->dev;
596 output = &par->output;
597 crtc = &par->crtc;
598
599 crtc->vidmem_size = sm750_dev->vidmem_size;
600 if (sm750_dev->fb_count > 1)
601 crtc->vidmem_size >>= 1;
602
603 /* setup crtc and output member */
604 sm750_dev->hwCursor = g_hwcursor;
605
606 crtc->line_pad = 16;
607 crtc->xpanstep = 8;
608 crtc->ypanstep = 1;
609 crtc->ywrapstep = 0;
610
611 output->proc_setBLANK = (sm750_dev->revid == SM750LE_REVISION_ID) ?
612 hw_sm750le_setBLANK : hw_sm750_setBLANK;
613 /* chip specific phase */
614 sm750_dev->accel.de_wait = (sm750_dev->revid == SM750LE_REVISION_ID) ?
615 hw_sm750le_deWait : hw_sm750_deWait;
616 switch (sm750_dev->dataflow) {
617 case sm750_simul_pri:
618 output->paths = sm750_pnc;
619 crtc->channel = sm750_primary;
620 crtc->o_screen = 0;
621 crtc->v_screen = sm750_dev->pvMem;
622 pr_info("use simul primary mode\n");
623 break;
624 case sm750_simul_sec:
625 output->paths = sm750_pnc;
626 crtc->channel = sm750_secondary;
627 crtc->o_screen = 0;
628 crtc->v_screen = sm750_dev->pvMem;
629 break;
630 case sm750_dual_normal:
631 if (par->index == 0) {
632 output->paths = sm750_panel;
633 crtc->channel = sm750_primary;
634 crtc->o_screen = 0;
635 crtc->v_screen = sm750_dev->pvMem;
636 } else {
637 output->paths = sm750_crt;
638 crtc->channel = sm750_secondary;
639 /* not consider of padding stuffs for o_screen,need fix */
640 crtc->o_screen = sm750_dev->vidmem_size >> 1;
641 crtc->v_screen = sm750_dev->pvMem + crtc->o_screen;
642 }
643 break;
644 case sm750_dual_swap:
645 if (par->index == 0) {
646 output->paths = sm750_panel;
647 crtc->channel = sm750_secondary;
648 crtc->o_screen = 0;
649 crtc->v_screen = sm750_dev->pvMem;
650 } else {
651 output->paths = sm750_crt;
652 crtc->channel = sm750_primary;
653 /* not consider of padding stuffs for o_screen,
654 * need fix
655 */
656 crtc->o_screen = sm750_dev->vidmem_size >> 1;
657 crtc->v_screen = sm750_dev->pvMem + crtc->o_screen;
658 }
659 break;
660 default:
661 ret = -EINVAL;
662 }
663
664 return ret;
665 }
666
667 static struct fb_ops lynxfb_ops = {
668 .owner = THIS_MODULE,
669 .fb_check_var = lynxfb_ops_check_var,
670 .fb_set_par = lynxfb_ops_set_par,
671 .fb_setcolreg = lynxfb_ops_setcolreg,
672 .fb_blank = lynxfb_ops_blank,
673 .fb_fillrect = cfb_fillrect,
674 .fb_imageblit = cfb_imageblit,
675 .fb_copyarea = cfb_copyarea,
676 /* cursor */
677 .fb_cursor = lynxfb_ops_cursor,
678 };
679
lynxfb_set_fbinfo(struct fb_info * info,int index)680 static int lynxfb_set_fbinfo(struct fb_info *info, int index)
681 {
682 int i;
683 struct lynxfb_par *par;
684 struct sm750_dev *sm750_dev;
685 struct lynxfb_crtc *crtc;
686 struct lynxfb_output *output;
687 struct fb_var_screeninfo *var;
688 struct fb_fix_screeninfo *fix;
689
690 const struct fb_videomode *pdb[] = {
691 lynx750_ext, NULL, vesa_modes,
692 };
693 int cdb[] = {ARRAY_SIZE(lynx750_ext), 0, VESA_MODEDB_SIZE};
694 static const char * const mdb_desc[] = {
695 "driver prepared modes",
696 "kernel prepared default modedb",
697 "kernel HELPERS prepared vesa_modes",
698 };
699
700 static const char *fixId[2] = {
701 "sm750_fb1", "sm750_fb2",
702 };
703
704 int ret, line_length;
705
706 ret = 0;
707 par = (struct lynxfb_par *)info->par;
708 sm750_dev = par->dev;
709 crtc = &par->crtc;
710 output = &par->output;
711 var = &info->var;
712 fix = &info->fix;
713
714 /* set index */
715 par->index = index;
716 output->channel = &crtc->channel;
717 sm750fb_set_drv(par);
718 lynxfb_ops.fb_pan_display = lynxfb_ops_pan_display;
719
720 /*
721 * set current cursor variable and proc pointer,
722 * must be set after crtc member initialized
723 */
724 crtc->cursor.offset = crtc->o_screen + crtc->vidmem_size - 1024;
725 crtc->cursor.mmio = sm750_dev->pvReg +
726 0x800f0 + (int)crtc->channel * 0x140;
727
728 pr_info("crtc->cursor.mmio = %p\n", crtc->cursor.mmio);
729 crtc->cursor.max_h = 64;
730 crtc->cursor.max_w = 64;
731 crtc->cursor.size = crtc->cursor.max_h * crtc->cursor.max_w * 2 / 8;
732 crtc->cursor.vstart = sm750_dev->pvMem + crtc->cursor.offset;
733
734 memset_io(crtc->cursor.vstart, 0, crtc->cursor.size);
735 if (!g_hwcursor) {
736 lynxfb_ops.fb_cursor = NULL;
737 sm750_hw_cursor_disable(&crtc->cursor);
738 }
739
740 /* set info->fbops, must be set before fb_find_mode */
741 if (!sm750_dev->accel_off) {
742 /* use 2d acceleration */
743 lynxfb_ops.fb_fillrect = lynxfb_ops_fillrect;
744 lynxfb_ops.fb_copyarea = lynxfb_ops_copyarea;
745 lynxfb_ops.fb_imageblit = lynxfb_ops_imageblit;
746 }
747 info->fbops = &lynxfb_ops;
748
749 if (!g_fbmode[index]) {
750 g_fbmode[index] = g_def_fbmode;
751 if (index)
752 g_fbmode[index] = g_fbmode[0];
753 }
754
755 for (i = 0; i < 3; i++) {
756 ret = fb_find_mode(var, info, g_fbmode[index],
757 pdb[i], cdb[i], NULL, 8);
758
759 if (ret == 1) {
760 pr_info("success! use specified mode:%s in %s\n",
761 g_fbmode[index],
762 mdb_desc[i]);
763 break;
764 } else if (ret == 2) {
765 pr_warn("use specified mode:%s in %s,with an ignored refresh rate\n",
766 g_fbmode[index],
767 mdb_desc[i]);
768 break;
769 } else if (ret == 3) {
770 pr_warn("wanna use default mode\n");
771 /*break;*/
772 } else if (ret == 4) {
773 pr_warn("fall back to any valid mode\n");
774 } else {
775 pr_warn("ret = %d,fb_find_mode failed,with %s\n",
776 ret,
777 mdb_desc[i]);
778 }
779 }
780
781 /* some member of info->var had been set by fb_find_mode */
782
783 pr_info("Member of info->var is :\n"
784 "xres=%d\n"
785 "yres=%d\n"
786 "xres_virtual=%d\n"
787 "yres_virtual=%d\n"
788 "xoffset=%d\n"
789 "yoffset=%d\n"
790 "bits_per_pixel=%d\n"
791 " ...\n",
792 var->xres,
793 var->yres,
794 var->xres_virtual,
795 var->yres_virtual,
796 var->xoffset,
797 var->yoffset,
798 var->bits_per_pixel);
799
800 /* set par */
801 par->info = info;
802
803 /* set info */
804 line_length = ALIGN((var->xres_virtual * var->bits_per_pixel / 8),
805 crtc->line_pad);
806
807 info->pseudo_palette = &par->pseudo_palette[0];
808 info->screen_base = crtc->v_screen;
809 pr_debug("screen_base vaddr = %p\n", info->screen_base);
810 info->screen_size = line_length * var->yres_virtual;
811 info->flags = FBINFO_FLAG_DEFAULT | 0;
812
813 /* set info->fix */
814 fix->type = FB_TYPE_PACKED_PIXELS;
815 fix->type_aux = 0;
816 fix->xpanstep = crtc->xpanstep;
817 fix->ypanstep = crtc->ypanstep;
818 fix->ywrapstep = crtc->ywrapstep;
819 fix->accel = FB_ACCEL_SMI;
820
821 strscpy(fix->id, fixId[index], sizeof(fix->id));
822
823 fix->smem_start = crtc->o_screen + sm750_dev->vidmem_start;
824 pr_info("fix->smem_start = %lx\n", fix->smem_start);
825 /*
826 * according to mmap experiment from user space application,
827 * fix->mmio_len should not larger than virtual size
828 * (xres_virtual x yres_virtual x ByPP)
829 * Below line maybe buggy when user mmap fb dev node and write
830 * data into the bound over virtual size
831 */
832 fix->smem_len = crtc->vidmem_size;
833 pr_info("fix->smem_len = %x\n", fix->smem_len);
834 info->screen_size = fix->smem_len;
835 fix->line_length = line_length;
836 fix->mmio_start = sm750_dev->vidreg_start;
837 pr_info("fix->mmio_start = %lx\n", fix->mmio_start);
838 fix->mmio_len = sm750_dev->vidreg_size;
839 pr_info("fix->mmio_len = %x\n", fix->mmio_len);
840
841 lynxfb_set_visual_mode(info);
842
843 /* set var */
844 var->activate = FB_ACTIVATE_NOW;
845 var->accel_flags = 0;
846 var->vmode = FB_VMODE_NONINTERLACED;
847
848 pr_debug("#1 show info->cmap :\nstart=%d,len=%d,red=%p,green=%p,blue=%p,transp=%p\n",
849 info->cmap.start, info->cmap.len,
850 info->cmap.red, info->cmap.green, info->cmap.blue,
851 info->cmap.transp);
852
853 ret = fb_alloc_cmap(&info->cmap, 256, 0);
854 if (ret < 0) {
855 pr_err("Could not allocate memory for cmap.\n");
856 goto exit;
857 }
858
859 pr_debug("#2 show info->cmap :\nstart=%d,len=%d,red=%p,green=%p,blue=%p,transp=%p\n",
860 info->cmap.start, info->cmap.len,
861 info->cmap.red, info->cmap.green, info->cmap.blue,
862 info->cmap.transp);
863
864 exit:
865 lynxfb_ops_check_var(var, info);
866 return ret;
867 }
868
869 /* chip specific g_option configuration routine */
sm750fb_setup(struct sm750_dev * sm750_dev,char * src)870 static void sm750fb_setup(struct sm750_dev *sm750_dev, char *src)
871 {
872 char *opt;
873 int swap;
874
875 swap = 0;
876
877 sm750_dev->initParm.chip_clk = 0;
878 sm750_dev->initParm.mem_clk = 0;
879 sm750_dev->initParm.master_clk = 0;
880 sm750_dev->initParm.powerMode = 0;
881 sm750_dev->initParm.setAllEngOff = 0;
882 sm750_dev->initParm.resetMemory = 1;
883
884 /* defaultly turn g_hwcursor on for both view */
885 g_hwcursor = 3;
886
887 if (!src || !*src) {
888 dev_warn(&sm750_dev->pdev->dev, "no specific g_option.\n");
889 goto NO_PARAM;
890 }
891
892 while ((opt = strsep(&src, ":")) != NULL && *opt != 0) {
893 dev_info(&sm750_dev->pdev->dev, "opt=%s\n", opt);
894 dev_info(&sm750_dev->pdev->dev, "src=%s\n", src);
895
896 if (!strncmp(opt, "swap", strlen("swap"))) {
897 swap = 1;
898 } else if (!strncmp(opt, "nocrt", strlen("nocrt"))) {
899 sm750_dev->nocrt = 1;
900 } else if (!strncmp(opt, "36bit", strlen("36bit"))) {
901 sm750_dev->pnltype = sm750_doubleTFT;
902 } else if (!strncmp(opt, "18bit", strlen("18bit"))) {
903 sm750_dev->pnltype = sm750_dualTFT;
904 } else if (!strncmp(opt, "24bit", strlen("24bit"))) {
905 sm750_dev->pnltype = sm750_24TFT;
906 } else if (!strncmp(opt, "nohwc0", strlen("nohwc0"))) {
907 g_hwcursor &= ~0x1;
908 } else if (!strncmp(opt, "nohwc1", strlen("nohwc1"))) {
909 g_hwcursor &= ~0x2;
910 } else if (!strncmp(opt, "nohwc", strlen("nohwc"))) {
911 g_hwcursor = 0;
912 } else {
913 if (!g_fbmode[0]) {
914 g_fbmode[0] = opt;
915 dev_info(&sm750_dev->pdev->dev,
916 "find fbmode0 : %s\n", g_fbmode[0]);
917 } else if (!g_fbmode[1]) {
918 g_fbmode[1] = opt;
919 dev_info(&sm750_dev->pdev->dev,
920 "find fbmode1 : %s\n", g_fbmode[1]);
921 } else {
922 dev_warn(&sm750_dev->pdev->dev, "How many view you wann set?\n");
923 }
924 }
925 }
926
927 NO_PARAM:
928 if (sm750_dev->revid != SM750LE_REVISION_ID) {
929 if (sm750_dev->fb_count > 1) {
930 if (swap)
931 sm750_dev->dataflow = sm750_dual_swap;
932 else
933 sm750_dev->dataflow = sm750_dual_normal;
934 } else {
935 if (swap)
936 sm750_dev->dataflow = sm750_simul_sec;
937 else
938 sm750_dev->dataflow = sm750_simul_pri;
939 }
940 } else {
941 /* SM750LE only have one crt channel */
942 sm750_dev->dataflow = sm750_simul_sec;
943 /* sm750le do not have complex attributes */
944 sm750_dev->nocrt = 0;
945 }
946 }
947
sm750fb_framebuffer_release(struct sm750_dev * sm750_dev)948 static void sm750fb_framebuffer_release(struct sm750_dev *sm750_dev)
949 {
950 struct fb_info *fb_info;
951
952 while (sm750_dev->fb_count) {
953 fb_info = sm750_dev->fbinfo[sm750_dev->fb_count - 1];
954 unregister_framebuffer(fb_info);
955 framebuffer_release(fb_info);
956 sm750_dev->fb_count--;
957 }
958 }
959
sm750fb_framebuffer_alloc(struct sm750_dev * sm750_dev,int fbidx)960 static int sm750fb_framebuffer_alloc(struct sm750_dev *sm750_dev, int fbidx)
961 {
962 struct fb_info *fb_info;
963 struct lynxfb_par *par;
964 int err;
965
966 fb_info = framebuffer_alloc(sizeof(struct lynxfb_par),
967 &sm750_dev->pdev->dev);
968 if (!fb_info)
969 return -ENOMEM;
970
971 sm750_dev->fbinfo[fbidx] = fb_info;
972 par = fb_info->par;
973 par->dev = sm750_dev;
974
975 err = lynxfb_set_fbinfo(fb_info, fbidx);
976 if (err)
977 goto release_fb;
978
979 err = register_framebuffer(fb_info);
980 if (err < 0)
981 goto release_fb;
982
983 sm750_dev->fb_count++;
984
985 return 0;
986
987 release_fb:
988 framebuffer_release(fb_info);
989 return err;
990 }
991
lynxfb_kick_out_firmware_fb(struct pci_dev * pdev)992 static int lynxfb_kick_out_firmware_fb(struct pci_dev *pdev)
993 {
994 resource_size_t base = pci_resource_start(pdev, 0);
995 resource_size_t size = pci_resource_len(pdev, 0);
996 bool primary = false;
997
998 #ifdef CONFIG_X86
999 primary = pdev->resource[PCI_ROM_RESOURCE].flags &
1000 IORESOURCE_ROM_SHADOW;
1001 #endif
1002
1003 return aperture_remove_conflicting_devices(base, size, primary, "sm750_fb1");
1004 }
1005
lynxfb_pci_probe(struct pci_dev * pdev,const struct pci_device_id * ent)1006 static int lynxfb_pci_probe(struct pci_dev *pdev,
1007 const struct pci_device_id *ent)
1008 {
1009 struct sm750_dev *sm750_dev = NULL;
1010 int max_fb;
1011 int fbidx;
1012 int err;
1013
1014 err = lynxfb_kick_out_firmware_fb(pdev);
1015 if (err)
1016 return err;
1017
1018 /* enable device */
1019 err = pcim_enable_device(pdev);
1020 if (err)
1021 return err;
1022
1023 err = -ENOMEM;
1024 sm750_dev = devm_kzalloc(&pdev->dev, sizeof(*sm750_dev), GFP_KERNEL);
1025 if (!sm750_dev)
1026 return err;
1027
1028 sm750_dev->fbinfo[0] = NULL;
1029 sm750_dev->fbinfo[1] = NULL;
1030 sm750_dev->devid = pdev->device;
1031 sm750_dev->revid = pdev->revision;
1032 sm750_dev->pdev = pdev;
1033 sm750_dev->mtrr_off = g_nomtrr;
1034 sm750_dev->mtrr.vram = 0;
1035 sm750_dev->accel_off = g_noaccel;
1036 spin_lock_init(&sm750_dev->slock);
1037
1038 if (!sm750_dev->accel_off) {
1039 /*
1040 * hook deInit and 2d routines, notes that below hw_xxx
1041 * routine can work on most of lynx chips
1042 * if some chip need specific function,
1043 * please hook it in smXXX_set_drv routine
1044 */
1045 sm750_dev->accel.de_init = sm750_hw_de_init;
1046 sm750_dev->accel.de_fillrect = sm750_hw_fillrect;
1047 sm750_dev->accel.de_copyarea = sm750_hw_copyarea;
1048 sm750_dev->accel.de_imageblit = sm750_hw_imageblit;
1049 }
1050
1051 /* call chip specific setup routine */
1052 sm750fb_setup(sm750_dev, g_settings);
1053
1054 /* call chip specific mmap routine */
1055 err = hw_sm750_map(sm750_dev, pdev);
1056 if (err)
1057 return err;
1058
1059 if (!sm750_dev->mtrr_off)
1060 sm750_dev->mtrr.vram = arch_phys_wc_add(sm750_dev->vidmem_start,
1061 sm750_dev->vidmem_size);
1062
1063 memset_io(sm750_dev->pvMem, 0, sm750_dev->vidmem_size);
1064
1065 pci_set_drvdata(pdev, sm750_dev);
1066
1067 /* call chipInit routine */
1068 hw_sm750_inithw(sm750_dev, pdev);
1069
1070 /* allocate frame buffer info structures according to g_dualview */
1071 max_fb = g_dualview ? 2 : 1;
1072 for (fbidx = 0; fbidx < max_fb; fbidx++) {
1073 err = sm750fb_framebuffer_alloc(sm750_dev, fbidx);
1074 if (err)
1075 goto release_fb;
1076 }
1077
1078 return 0;
1079
1080 release_fb:
1081 sm750fb_framebuffer_release(sm750_dev);
1082 return err;
1083 }
1084
lynxfb_pci_remove(struct pci_dev * pdev)1085 static void lynxfb_pci_remove(struct pci_dev *pdev)
1086 {
1087 struct sm750_dev *sm750_dev;
1088
1089 sm750_dev = pci_get_drvdata(pdev);
1090
1091 sm750fb_framebuffer_release(sm750_dev);
1092 arch_phys_wc_del(sm750_dev->mtrr.vram);
1093
1094 iounmap(sm750_dev->pvReg);
1095 iounmap(sm750_dev->pvMem);
1096 kfree(g_settings);
1097 }
1098
lynxfb_setup(char * options)1099 static int __init lynxfb_setup(char *options)
1100 {
1101 int len;
1102 char *opt, *tmp;
1103
1104 if (!options || !*options) {
1105 pr_warn("no options.\n");
1106 return 0;
1107 }
1108
1109 pr_info("options:%s\n", options);
1110
1111 len = strlen(options) + 1;
1112 g_settings = kzalloc(len, GFP_KERNEL);
1113 if (!g_settings)
1114 return -ENOMEM;
1115
1116 tmp = g_settings;
1117
1118 /*
1119 * Notes:
1120 * char * strsep(char **s,const char * ct);
1121 * @s: the string to be searched
1122 * @ct :the characters to search for
1123 *
1124 * strsep() updates @options to pointer after the first found token
1125 * it also returns the pointer ahead the token.
1126 */
1127 while ((opt = strsep(&options, ":")) != NULL) {
1128 /* options that mean for any lynx chips are configured here */
1129 if (!strncmp(opt, "noaccel", strlen("noaccel"))) {
1130 g_noaccel = 1;
1131 } else if (!strncmp(opt, "nomtrr", strlen("nomtrr"))) {
1132 g_nomtrr = 1;
1133 } else if (!strncmp(opt, "dual", strlen("dual"))) {
1134 g_dualview = 1;
1135 } else {
1136 strcat(tmp, opt);
1137 tmp += strlen(opt);
1138 if (options)
1139 *tmp++ = ':';
1140 else
1141 *tmp++ = 0;
1142 }
1143 }
1144
1145 /* misc g_settings are transport to chip specific routines */
1146 pr_info("parameter left for chip specific analysis:%s\n", g_settings);
1147 return 0;
1148 }
1149
1150 static const struct pci_device_id smi_pci_table[] = {
1151 { PCI_DEVICE(0x126f, 0x0750), },
1152 {0,}
1153 };
1154
1155 MODULE_DEVICE_TABLE(pci, smi_pci_table);
1156
1157 static SIMPLE_DEV_PM_OPS(lynxfb_pm_ops, lynxfb_suspend, lynxfb_resume);
1158
1159 static struct pci_driver lynxfb_driver = {
1160 .name = "sm750fb",
1161 .id_table = smi_pci_table,
1162 .probe = lynxfb_pci_probe,
1163 .remove = lynxfb_pci_remove,
1164 .driver.pm = &lynxfb_pm_ops,
1165 };
1166
lynxfb_init(void)1167 static int __init lynxfb_init(void)
1168 {
1169 char *option;
1170
1171 if (fb_modesetting_disabled("sm750fb"))
1172 return -ENODEV;
1173
1174 #ifdef MODULE
1175 option = g_option;
1176 #else
1177 if (fb_get_options("sm750fb", &option))
1178 return -ENODEV;
1179 #endif
1180
1181 lynxfb_setup(option);
1182 return pci_register_driver(&lynxfb_driver);
1183 }
1184 module_init(lynxfb_init);
1185
lynxfb_exit(void)1186 static void __exit lynxfb_exit(void)
1187 {
1188 pci_unregister_driver(&lynxfb_driver);
1189 }
1190 module_exit(lynxfb_exit);
1191
1192 module_param(g_option, charp, 0444);
1193
1194 MODULE_PARM_DESC(g_option,
1195 "\n\t\tCommon options:\n"
1196 "\t\tnoaccel:disable 2d capabilities\n"
1197 "\t\tnomtrr:disable MTRR attribute for video memory\n"
1198 "\t\tdualview:dual frame buffer feature enabled\n"
1199 "\t\tnohwc:disable hardware cursor\n"
1200 "\t\tUsual example:\n"
1201 "\t\tinsmod ./sm750fb.ko g_option=\"noaccel,nohwc,1280x1024-8@60\"\n"
1202 );
1203
1204 MODULE_AUTHOR("monk liu <monk.liu@siliconmotion.com>");
1205 MODULE_AUTHOR("Sudip Mukherjee <sudip@vectorindia.org>");
1206 MODULE_DESCRIPTION("Frame buffer driver for SM750 chipset");
1207 MODULE_LICENSE("Dual BSD/GPL");
1208