1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/usb.h>
29#include <linux/uaccess.h>
30#include <linux/mm.h>
31#include <linux/fb.h>
32#include <linux/vmalloc.h>
33#include <linux/slab.h>
34#include <linux/delay.h>
35#include "edid.h"
36
37#define check_warn(status, fmt, args...) \
38 ({ if (status < 0) pr_warn(fmt, ##args); })
39
40#define check_warn_return(status, fmt, args...) \
41 ({ if (status < 0) { pr_warn(fmt, ##args); return status; } })
42
43#define check_warn_goto_error(status, fmt, args...) \
44 ({ if (status < 0) { pr_warn(fmt, ##args); goto error; } })
45
46#define all_bits_set(x, bits) (((x) & (bits)) == (bits))
47
48#define USB_VENDOR_REQUEST_WRITE_REGISTER 0xA0
49#define USB_VENDOR_REQUEST_READ_REGISTER 0xA1
50
51
52
53
54
55
56
57
58#define UFX_IOCTL_RETURN_EDID (0xAD)
59#define UFX_IOCTL_REPORT_DAMAGE (0xAA)
60
61
62#define BULK_SIZE (512)
63#define MAX_TRANSFER (PAGE_SIZE*16 - BULK_SIZE)
64#define WRITES_IN_FLIGHT (4)
65
66#define GET_URB_TIMEOUT (HZ)
67#define FREE_URB_TIMEOUT (HZ*2)
68
69#define BPP 2
70
71#define UFX_DEFIO_WRITE_DELAY 5
72#define UFX_DEFIO_WRITE_DISABLE (HZ*60)
73
74struct dloarea {
75 int x, y;
76 int w, h;
77};
78
79struct urb_node {
80 struct list_head entry;
81 struct ufx_data *dev;
82 struct delayed_work release_urb_work;
83 struct urb *urb;
84};
85
86struct urb_list {
87 struct list_head list;
88 spinlock_t lock;
89 struct semaphore limit_sem;
90 int available;
91 int count;
92 size_t size;
93};
94
95struct ufx_data {
96 struct usb_device *udev;
97 struct device *gdev;
98 struct fb_info *info;
99 struct urb_list urbs;
100 struct kref kref;
101 int fb_count;
102 bool virtualized;
103 struct delayed_work free_framebuffer_work;
104 atomic_t usb_active;
105 atomic_t lost_pixels;
106 u8 *edid;
107 size_t edid_size;
108 u32 pseudo_palette[256];
109};
110
111static struct fb_fix_screeninfo ufx_fix = {
112 .id = "smscufx",
113 .type = FB_TYPE_PACKED_PIXELS,
114 .visual = FB_VISUAL_TRUECOLOR,
115 .xpanstep = 0,
116 .ypanstep = 0,
117 .ywrapstep = 0,
118 .accel = FB_ACCEL_NONE,
119};
120
121static const u32 smscufx_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
122 FBINFO_VIRTFB | FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
123 FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
124
125static struct usb_device_id id_table[] = {
126 {USB_DEVICE(0x0424, 0x9d00),},
127 {USB_DEVICE(0x0424, 0x9d01),},
128 {},
129};
130MODULE_DEVICE_TABLE(usb, id_table);
131
132
133static bool console;
134static bool fb_defio = true;
135
136
137static void ufx_urb_completion(struct urb *urb);
138static struct urb *ufx_get_urb(struct ufx_data *dev);
139static int ufx_submit_urb(struct ufx_data *dev, struct urb * urb, size_t len);
140static int ufx_alloc_urb_list(struct ufx_data *dev, int count, size_t size);
141static void ufx_free_urb_list(struct ufx_data *dev);
142
143
144static int ufx_reg_read(struct ufx_data *dev, u32 index, u32 *data)
145{
146 u32 *buf = kmalloc(4, GFP_KERNEL);
147 int ret;
148
149 BUG_ON(!dev);
150
151 if (!buf)
152 return -ENOMEM;
153
154 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
155 USB_VENDOR_REQUEST_READ_REGISTER,
156 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
157 00, index, buf, 4, USB_CTRL_GET_TIMEOUT);
158
159 le32_to_cpus(buf);
160 *data = *buf;
161 kfree(buf);
162
163 if (unlikely(ret < 0))
164 pr_warn("Failed to read register index 0x%08x\n", index);
165
166 return ret;
167}
168
169
170static int ufx_reg_write(struct ufx_data *dev, u32 index, u32 data)
171{
172 u32 *buf = kmalloc(4, GFP_KERNEL);
173 int ret;
174
175 BUG_ON(!dev);
176
177 if (!buf)
178 return -ENOMEM;
179
180 *buf = data;
181 cpu_to_le32s(buf);
182
183 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
184 USB_VENDOR_REQUEST_WRITE_REGISTER,
185 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
186 00, index, buf, 4, USB_CTRL_SET_TIMEOUT);
187
188 kfree(buf);
189
190 if (unlikely(ret < 0))
191 pr_warn("Failed to write register index 0x%08x with value "
192 "0x%08x\n", index, data);
193
194 return ret;
195}
196
197static int ufx_reg_clear_and_set_bits(struct ufx_data *dev, u32 index,
198 u32 bits_to_clear, u32 bits_to_set)
199{
200 u32 data;
201 int status = ufx_reg_read(dev, index, &data);
202 check_warn_return(status, "ufx_reg_clear_and_set_bits error reading "
203 "0x%x", index);
204
205 data &= (~bits_to_clear);
206 data |= bits_to_set;
207
208 status = ufx_reg_write(dev, index, data);
209 check_warn_return(status, "ufx_reg_clear_and_set_bits error writing "
210 "0x%x", index);
211
212 return 0;
213}
214
215static int ufx_reg_set_bits(struct ufx_data *dev, u32 index, u32 bits)
216{
217 return ufx_reg_clear_and_set_bits(dev, index, 0, bits);
218}
219
220static int ufx_reg_clear_bits(struct ufx_data *dev, u32 index, u32 bits)
221{
222 return ufx_reg_clear_and_set_bits(dev, index, bits, 0);
223}
224
225static int ufx_lite_reset(struct ufx_data *dev)
226{
227 int status;
228 u32 value;
229
230 status = ufx_reg_write(dev, 0x3008, 0x00000001);
231 check_warn_return(status, "ufx_lite_reset error writing 0x3008");
232
233 status = ufx_reg_read(dev, 0x3008, &value);
234 check_warn_return(status, "ufx_lite_reset error reading 0x3008");
235
236 return (value == 0) ? 0 : -EIO;
237}
238
239
240static int ufx_blank(struct ufx_data *dev, bool wait)
241{
242 u32 dc_ctrl, dc_sts;
243 int i;
244
245 int status = ufx_reg_read(dev, 0x2004, &dc_sts);
246 check_warn_return(status, "ufx_blank error reading 0x2004");
247
248 status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
249 check_warn_return(status, "ufx_blank error reading 0x2000");
250
251
252 if ((dc_sts & 0x00000100) || (dc_ctrl & 0x00000100))
253 return 0;
254
255
256 dc_ctrl |= 0x00000100;
257 status = ufx_reg_write(dev, 0x2000, dc_ctrl);
258 check_warn_return(status, "ufx_blank error writing 0x2000");
259
260
261 if (!wait)
262 return 0;
263
264 for (i = 0; i < 250; i++) {
265 status = ufx_reg_read(dev, 0x2004, &dc_sts);
266 check_warn_return(status, "ufx_blank error reading 0x2004");
267
268 if (dc_sts & 0x00000100)
269 return 0;
270 }
271
272
273 return -EIO;
274}
275
276
277static int ufx_unblank(struct ufx_data *dev, bool wait)
278{
279 u32 dc_ctrl, dc_sts;
280 int i;
281
282 int status = ufx_reg_read(dev, 0x2004, &dc_sts);
283 check_warn_return(status, "ufx_unblank error reading 0x2004");
284
285 status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
286 check_warn_return(status, "ufx_unblank error reading 0x2000");
287
288
289 if (((dc_sts & 0x00000100) == 0) || ((dc_ctrl & 0x00000100) == 0))
290 return 0;
291
292
293 dc_ctrl &= ~0x00000100;
294 status = ufx_reg_write(dev, 0x2000, dc_ctrl);
295 check_warn_return(status, "ufx_unblank error writing 0x2000");
296
297
298 if (!wait)
299 return 0;
300
301 for (i = 0; i < 250; i++) {
302 status = ufx_reg_read(dev, 0x2004, &dc_sts);
303 check_warn_return(status, "ufx_unblank error reading 0x2004");
304
305 if ((dc_sts & 0x00000100) == 0)
306 return 0;
307 }
308
309
310 return -EIO;
311}
312
313
314static int ufx_disable(struct ufx_data *dev, bool wait)
315{
316 u32 dc_ctrl, dc_sts;
317 int i;
318
319 int status = ufx_reg_read(dev, 0x2004, &dc_sts);
320 check_warn_return(status, "ufx_disable error reading 0x2004");
321
322 status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
323 check_warn_return(status, "ufx_disable error reading 0x2000");
324
325
326 if (((dc_sts & 0x00000001) == 0) || ((dc_ctrl & 0x00000001) == 0))
327 return 0;
328
329
330 dc_ctrl &= ~(0x00000001);
331 status = ufx_reg_write(dev, 0x2000, dc_ctrl);
332 check_warn_return(status, "ufx_disable error writing 0x2000");
333
334
335 if (!wait)
336 return 0;
337
338 for (i = 0; i < 250; i++) {
339 status = ufx_reg_read(dev, 0x2004, &dc_sts);
340 check_warn_return(status, "ufx_disable error reading 0x2004");
341
342 if ((dc_sts & 0x00000001) == 0)
343 return 0;
344 }
345
346
347 return -EIO;
348}
349
350
351static int ufx_enable(struct ufx_data *dev, bool wait)
352{
353 u32 dc_ctrl, dc_sts;
354 int i;
355
356 int status = ufx_reg_read(dev, 0x2004, &dc_sts);
357 check_warn_return(status, "ufx_enable error reading 0x2004");
358
359 status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
360 check_warn_return(status, "ufx_enable error reading 0x2000");
361
362
363 if ((dc_sts & 0x00000001) || (dc_ctrl & 0x00000001))
364 return 0;
365
366
367 dc_ctrl |= 0x00000001;
368 status = ufx_reg_write(dev, 0x2000, dc_ctrl);
369 check_warn_return(status, "ufx_enable error writing 0x2000");
370
371
372 if (!wait)
373 return 0;
374
375 for (i = 0; i < 250; i++) {
376 status = ufx_reg_read(dev, 0x2004, &dc_sts);
377 check_warn_return(status, "ufx_enable error reading 0x2004");
378
379 if (dc_sts & 0x00000001)
380 return 0;
381 }
382
383
384 return -EIO;
385}
386
387static int ufx_config_sys_clk(struct ufx_data *dev)
388{
389 int status = ufx_reg_write(dev, 0x700C, 0x8000000F);
390 check_warn_return(status, "error writing 0x700C");
391
392 status = ufx_reg_write(dev, 0x7014, 0x0010024F);
393 check_warn_return(status, "error writing 0x7014");
394
395 status = ufx_reg_write(dev, 0x7010, 0x00000000);
396 check_warn_return(status, "error writing 0x7010");
397
398 status = ufx_reg_clear_bits(dev, 0x700C, 0x0000000A);
399 check_warn_return(status, "error clearing PLL1 bypass in 0x700C");
400 msleep(1);
401
402 status = ufx_reg_clear_bits(dev, 0x700C, 0x80000000);
403 check_warn_return(status, "error clearing output gate in 0x700C");
404
405 return 0;
406}
407
408static int ufx_config_ddr2(struct ufx_data *dev)
409{
410 int status, i = 0;
411 u32 tmp;
412
413 status = ufx_reg_write(dev, 0x0004, 0x001F0F77);
414 check_warn_return(status, "error writing 0x0004");
415
416 status = ufx_reg_write(dev, 0x0008, 0xFFF00000);
417 check_warn_return(status, "error writing 0x0008");
418
419 status = ufx_reg_write(dev, 0x000C, 0x0FFF2222);
420 check_warn_return(status, "error writing 0x000C");
421
422 status = ufx_reg_write(dev, 0x0010, 0x00030814);
423 check_warn_return(status, "error writing 0x0010");
424
425 status = ufx_reg_write(dev, 0x0014, 0x00500019);
426 check_warn_return(status, "error writing 0x0014");
427
428 status = ufx_reg_write(dev, 0x0018, 0x020D0F15);
429 check_warn_return(status, "error writing 0x0018");
430
431 status = ufx_reg_write(dev, 0x001C, 0x02532305);
432 check_warn_return(status, "error writing 0x001C");
433
434 status = ufx_reg_write(dev, 0x0020, 0x0B030905);
435 check_warn_return(status, "error writing 0x0020");
436
437 status = ufx_reg_write(dev, 0x0024, 0x00000827);
438 check_warn_return(status, "error writing 0x0024");
439
440 status = ufx_reg_write(dev, 0x0028, 0x00000000);
441 check_warn_return(status, "error writing 0x0028");
442
443 status = ufx_reg_write(dev, 0x002C, 0x00000042);
444 check_warn_return(status, "error writing 0x002C");
445
446 status = ufx_reg_write(dev, 0x0030, 0x09520000);
447 check_warn_return(status, "error writing 0x0030");
448
449 status = ufx_reg_write(dev, 0x0034, 0x02223314);
450 check_warn_return(status, "error writing 0x0034");
451
452 status = ufx_reg_write(dev, 0x0038, 0x00430043);
453 check_warn_return(status, "error writing 0x0038");
454
455 status = ufx_reg_write(dev, 0x003C, 0xF00F000F);
456 check_warn_return(status, "error writing 0x003C");
457
458 status = ufx_reg_write(dev, 0x0040, 0xF380F00F);
459 check_warn_return(status, "error writing 0x0040");
460
461 status = ufx_reg_write(dev, 0x0044, 0xF00F0496);
462 check_warn_return(status, "error writing 0x0044");
463
464 status = ufx_reg_write(dev, 0x0048, 0x03080406);
465 check_warn_return(status, "error writing 0x0048");
466
467 status = ufx_reg_write(dev, 0x004C, 0x00001000);
468 check_warn_return(status, "error writing 0x004C");
469
470 status = ufx_reg_write(dev, 0x005C, 0x00000007);
471 check_warn_return(status, "error writing 0x005C");
472
473 status = ufx_reg_write(dev, 0x0100, 0x54F00012);
474 check_warn_return(status, "error writing 0x0100");
475
476 status = ufx_reg_write(dev, 0x0104, 0x00004012);
477 check_warn_return(status, "error writing 0x0104");
478
479 status = ufx_reg_write(dev, 0x0118, 0x40404040);
480 check_warn_return(status, "error writing 0x0118");
481
482 status = ufx_reg_write(dev, 0x0000, 0x00000001);
483 check_warn_return(status, "error writing 0x0000");
484
485 while (i++ < 500) {
486 status = ufx_reg_read(dev, 0x0000, &tmp);
487 check_warn_return(status, "error reading 0x0000");
488
489 if (all_bits_set(tmp, 0xC0000000))
490 return 0;
491 }
492
493 pr_err("DDR2 initialisation timed out, reg 0x0000=0x%08x", tmp);
494 return -ETIMEDOUT;
495}
496
497struct pll_values {
498 u32 div_r0;
499 u32 div_f0;
500 u32 div_q0;
501 u32 range0;
502 u32 div_r1;
503 u32 div_f1;
504 u32 div_q1;
505 u32 range1;
506};
507
508static u32 ufx_calc_range(u32 ref_freq)
509{
510 if (ref_freq >= 88000000)
511 return 7;
512
513 if (ref_freq >= 54000000)
514 return 6;
515
516 if (ref_freq >= 34000000)
517 return 5;
518
519 if (ref_freq >= 21000000)
520 return 4;
521
522 if (ref_freq >= 13000000)
523 return 3;
524
525 if (ref_freq >= 8000000)
526 return 2;
527
528 return 1;
529}
530
531
532static void ufx_calc_pll_values(const u32 clk_pixel_pll, struct pll_values *asic_pll)
533{
534 const u32 ref_clk = 25000000;
535 u32 div_r0, div_f0, div_q0, div_r1, div_f1, div_q1;
536 u32 min_error = clk_pixel_pll;
537
538 for (div_r0 = 1; div_r0 <= 32; div_r0++) {
539 u32 ref_freq0 = ref_clk / div_r0;
540 if (ref_freq0 < 5000000)
541 break;
542
543 if (ref_freq0 > 200000000)
544 continue;
545
546 for (div_f0 = 1; div_f0 <= 256; div_f0++) {
547 u32 vco_freq0 = ref_freq0 * div_f0;
548
549 if (vco_freq0 < 350000000)
550 continue;
551
552 if (vco_freq0 > 700000000)
553 break;
554
555 for (div_q0 = 0; div_q0 < 7; div_q0++) {
556 u32 pllout_freq0 = vco_freq0 / (1 << div_q0);
557
558 if (pllout_freq0 < 5000000)
559 break;
560
561 if (pllout_freq0 > 200000000)
562 continue;
563
564 for (div_r1 = 1; div_r1 <= 32; div_r1++) {
565 u32 ref_freq1 = pllout_freq0 / div_r1;
566
567 if (ref_freq1 < 5000000)
568 break;
569
570 for (div_f1 = 1; div_f1 <= 256; div_f1++) {
571 u32 vco_freq1 = ref_freq1 * div_f1;
572
573 if (vco_freq1 < 350000000)
574 continue;
575
576 if (vco_freq1 > 700000000)
577 break;
578
579 for (div_q1 = 0; div_q1 < 7; div_q1++) {
580 u32 pllout_freq1 = vco_freq1 / (1 << div_q1);
581 int error = abs(pllout_freq1 - clk_pixel_pll);
582
583 if (pllout_freq1 < 5000000)
584 break;
585
586 if (pllout_freq1 > 700000000)
587 continue;
588
589 if (error < min_error) {
590 min_error = error;
591
592
593
594 asic_pll->div_r0 = div_r0 - 1;
595 asic_pll->div_f0 = div_f0 - 1;
596 asic_pll->div_q0 = div_q0;
597 asic_pll->div_r1 = div_r1 - 1;
598 asic_pll->div_f1 = div_f1 - 1;
599 asic_pll->div_q1 = div_q1;
600
601 asic_pll->range0 = ufx_calc_range(ref_freq0);
602 asic_pll->range1 = ufx_calc_range(ref_freq1);
603
604 if (min_error == 0)
605 return;
606 }
607 }
608 }
609 }
610 }
611 }
612 }
613}
614
615
616static int ufx_config_pix_clk(struct ufx_data *dev, u32 pixclock)
617{
618 struct pll_values asic_pll = {0};
619 u32 value, clk_pixel, clk_pixel_pll;
620 int status;
621
622
623 clk_pixel = PICOS2KHZ(pixclock) * 1000;
624 pr_debug("pixclock %d ps = clk_pixel %d Hz", pixclock, clk_pixel);
625
626
627 clk_pixel_pll = clk_pixel * 2;
628
629 ufx_calc_pll_values(clk_pixel_pll, &asic_pll);
630
631
632 status = ufx_reg_write(dev, 0x7000, 0x8000000F);
633 check_warn_return(status, "error writing 0x7000");
634
635 value = (asic_pll.div_f1 | (asic_pll.div_r1 << 8) |
636 (asic_pll.div_q1 << 16) | (asic_pll.range1 << 20));
637 status = ufx_reg_write(dev, 0x7008, value);
638 check_warn_return(status, "error writing 0x7008");
639
640 value = (asic_pll.div_f0 | (asic_pll.div_r0 << 8) |
641 (asic_pll.div_q0 << 16) | (asic_pll.range0 << 20));
642 status = ufx_reg_write(dev, 0x7004, value);
643 check_warn_return(status, "error writing 0x7004");
644
645 status = ufx_reg_clear_bits(dev, 0x7000, 0x00000005);
646 check_warn_return(status,
647 "error clearing PLL0 bypass bits in 0x7000");
648 msleep(1);
649
650 status = ufx_reg_clear_bits(dev, 0x7000, 0x0000000A);
651 check_warn_return(status,
652 "error clearing PLL1 bypass bits in 0x7000");
653 msleep(1);
654
655 status = ufx_reg_clear_bits(dev, 0x7000, 0x80000000);
656 check_warn_return(status, "error clearing gate bits in 0x7000");
657
658 return 0;
659}
660
661static int ufx_set_vid_mode(struct ufx_data *dev, struct fb_var_screeninfo *var)
662{
663 u32 temp;
664 u16 h_total, h_active, h_blank_start, h_blank_end, h_sync_start, h_sync_end;
665 u16 v_total, v_active, v_blank_start, v_blank_end, v_sync_start, v_sync_end;
666
667 int status = ufx_reg_write(dev, 0x8028, 0);
668 check_warn_return(status, "ufx_set_vid_mode error disabling RGB pad");
669
670 status = ufx_reg_write(dev, 0x8024, 0);
671 check_warn_return(status, "ufx_set_vid_mode error disabling VDAC");
672
673
674 status = ufx_blank(dev, true);
675 check_warn_return(status, "ufx_set_vid_mode error blanking display");
676
677 status = ufx_disable(dev, true);
678 check_warn_return(status, "ufx_set_vid_mode error disabling display");
679
680 status = ufx_config_pix_clk(dev, var->pixclock);
681 check_warn_return(status, "ufx_set_vid_mode error configuring pixclock");
682
683 status = ufx_reg_write(dev, 0x2000, 0x00000104);
684 check_warn_return(status, "ufx_set_vid_mode error writing 0x2000");
685
686
687 h_total = var->xres + var->right_margin + var->hsync_len + var->left_margin;
688 h_active = var->xres;
689 h_blank_start = var->xres + var->right_margin;
690 h_blank_end = var->xres + var->right_margin + var->hsync_len;
691 h_sync_start = var->xres + var->right_margin;
692 h_sync_end = var->xres + var->right_margin + var->hsync_len;
693
694 temp = ((h_total - 1) << 16) | (h_active - 1);
695 status = ufx_reg_write(dev, 0x2008, temp);
696 check_warn_return(status, "ufx_set_vid_mode error writing 0x2008");
697
698 temp = ((h_blank_start - 1) << 16) | (h_blank_end - 1);
699 status = ufx_reg_write(dev, 0x200C, temp);
700 check_warn_return(status, "ufx_set_vid_mode error writing 0x200C");
701
702 temp = ((h_sync_start - 1) << 16) | (h_sync_end - 1);
703 status = ufx_reg_write(dev, 0x2010, temp);
704 check_warn_return(status, "ufx_set_vid_mode error writing 0x2010");
705
706
707 v_total = var->upper_margin + var->yres + var->lower_margin + var->vsync_len;
708 v_active = var->yres;
709 v_blank_start = var->yres + var->lower_margin;
710 v_blank_end = var->yres + var->lower_margin + var->vsync_len;
711 v_sync_start = var->yres + var->lower_margin;
712 v_sync_end = var->yres + var->lower_margin + var->vsync_len;
713
714 temp = ((v_total - 1) << 16) | (v_active - 1);
715 status = ufx_reg_write(dev, 0x2014, temp);
716 check_warn_return(status, "ufx_set_vid_mode error writing 0x2014");
717
718 temp = ((v_blank_start - 1) << 16) | (v_blank_end - 1);
719 status = ufx_reg_write(dev, 0x2018, temp);
720 check_warn_return(status, "ufx_set_vid_mode error writing 0x2018");
721
722 temp = ((v_sync_start - 1) << 16) | (v_sync_end - 1);
723 status = ufx_reg_write(dev, 0x201C, temp);
724 check_warn_return(status, "ufx_set_vid_mode error writing 0x201C");
725
726 status = ufx_reg_write(dev, 0x2020, 0x00000000);
727 check_warn_return(status, "ufx_set_vid_mode error writing 0x2020");
728
729 status = ufx_reg_write(dev, 0x2024, 0x00000000);
730 check_warn_return(status, "ufx_set_vid_mode error writing 0x2024");
731
732
733 temp = var->xres * var->yres * 2;
734 temp = (temp + 7) & (~0x7);
735 status = ufx_reg_write(dev, 0x2028, temp);
736 check_warn_return(status, "ufx_set_vid_mode error writing 0x2028");
737
738
739 status = ufx_reg_write(dev, 0x2040, 0);
740 check_warn_return(status, "ufx_set_vid_mode error writing 0x2040");
741
742 status = ufx_reg_write(dev, 0x2044, 0);
743 check_warn_return(status, "ufx_set_vid_mode error writing 0x2044");
744
745 status = ufx_reg_write(dev, 0x2048, 0);
746 check_warn_return(status, "ufx_set_vid_mode error writing 0x2048");
747
748
749 temp = 0x00000001;
750 if (var->sync & FB_SYNC_HOR_HIGH_ACT)
751 temp |= 0x00000010;
752
753 if (var->sync & FB_SYNC_VERT_HIGH_ACT)
754 temp |= 0x00000008;
755
756 status = ufx_reg_write(dev, 0x2040, temp);
757 check_warn_return(status, "ufx_set_vid_mode error writing 0x2040");
758
759
760 status = ufx_enable(dev, true);
761 check_warn_return(status, "ufx_set_vid_mode error enabling display");
762
763
764 status = ufx_unblank(dev, true);
765 check_warn_return(status, "ufx_set_vid_mode error unblanking display");
766
767
768 status = ufx_reg_write(dev, 0x8028, 0x00000003);
769 check_warn_return(status, "ufx_set_vid_mode error enabling RGB pad");
770
771
772 status = ufx_reg_write(dev, 0x8024, 0x00000007);
773 check_warn_return(status, "ufx_set_vid_mode error enabling VDAC");
774
775 return 0;
776}
777
778static int ufx_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
779{
780 unsigned long start = vma->vm_start;
781 unsigned long size = vma->vm_end - vma->vm_start;
782 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
783 unsigned long page, pos;
784
785 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
786 return -EINVAL;
787 if (size > info->fix.smem_len)
788 return -EINVAL;
789 if (offset > info->fix.smem_len - size)
790 return -EINVAL;
791
792 pos = (unsigned long)info->fix.smem_start + offset;
793
794 pr_debug("mmap() framebuffer addr:%lu size:%lu\n",
795 pos, size);
796
797 while (size > 0) {
798 page = vmalloc_to_pfn((void *)pos);
799 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
800 return -EAGAIN;
801
802 start += PAGE_SIZE;
803 pos += PAGE_SIZE;
804 if (size > PAGE_SIZE)
805 size -= PAGE_SIZE;
806 else
807 size = 0;
808 }
809
810 return 0;
811}
812
813static void ufx_raw_rect(struct ufx_data *dev, u16 *cmd, int x, int y,
814 int width, int height)
815{
816 size_t packed_line_len = ALIGN((width * 2), 4);
817 size_t packed_rect_len = packed_line_len * height;
818 int line;
819
820 BUG_ON(!dev);
821 BUG_ON(!dev->info);
822
823
824 *((u32 *)&cmd[0]) = cpu_to_le32(0x01);
825
826
827 *((u32 *)&cmd[2]) = cpu_to_le32(packed_rect_len + 16);
828
829 cmd[4] = cpu_to_le16(x);
830 cmd[5] = cpu_to_le16(y);
831 cmd[6] = cpu_to_le16(width);
832 cmd[7] = cpu_to_le16(height);
833
834
835 *((u32 *)&cmd[8]) = cpu_to_le32(0);
836
837
838 cmd[10] = cpu_to_le16(0x4000 | dev->info->var.xres);
839
840
841 cmd[11] = cpu_to_le16(dev->info->var.yres);
842
843
844 for (line = 0; line < height; line++) {
845 const int line_offset = dev->info->fix.line_length * (y + line);
846 const int byte_offset = line_offset + (x * BPP);
847 memcpy(&cmd[(24 + (packed_line_len * line)) / 2],
848 (char *)dev->info->fix.smem_start + byte_offset, width * BPP);
849 }
850}
851
852static int ufx_handle_damage(struct ufx_data *dev, int x, int y,
853 int width, int height)
854{
855 size_t packed_line_len = ALIGN((width * 2), 4);
856 int len, status, urb_lines, start_line = 0;
857
858 if ((width <= 0) || (height <= 0) ||
859 (x + width > dev->info->var.xres) ||
860 (y + height > dev->info->var.yres))
861 return -EINVAL;
862
863 if (!atomic_read(&dev->usb_active))
864 return 0;
865
866 while (start_line < height) {
867 struct urb *urb = ufx_get_urb(dev);
868 if (!urb) {
869 pr_warn("ufx_handle_damage unable to get urb");
870 return 0;
871 }
872
873
874 BUG_ON(urb->transfer_buffer_length < (24 + (width * 2)));
875
876
877 urb_lines = (urb->transfer_buffer_length - 24) / packed_line_len;
878
879
880 urb_lines = min(urb_lines, (height - start_line));
881
882 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
883
884 ufx_raw_rect(dev, urb->transfer_buffer, x, (y + start_line), width, urb_lines);
885 len = 24 + (packed_line_len * urb_lines);
886
887 status = ufx_submit_urb(dev, urb, len);
888 check_warn_return(status, "Error submitting URB");
889
890 start_line += urb_lines;
891 }
892
893 return 0;
894}
895
896
897
898
899
900static ssize_t ufx_ops_write(struct fb_info *info, const char __user *buf,
901 size_t count, loff_t *ppos)
902{
903 ssize_t result;
904 struct ufx_data *dev = info->par;
905 u32 offset = (u32) *ppos;
906
907 result = fb_sys_write(info, buf, count, ppos);
908
909 if (result > 0) {
910 int start = max((int)(offset / info->fix.line_length), 0);
911 int lines = min((u32)((result / info->fix.line_length) + 1),
912 (u32)info->var.yres);
913
914 ufx_handle_damage(dev, 0, start, info->var.xres, lines);
915 }
916
917 return result;
918}
919
920static void ufx_ops_copyarea(struct fb_info *info,
921 const struct fb_copyarea *area)
922{
923
924 struct ufx_data *dev = info->par;
925
926 sys_copyarea(info, area);
927
928 ufx_handle_damage(dev, area->dx, area->dy,
929 area->width, area->height);
930}
931
932static void ufx_ops_imageblit(struct fb_info *info,
933 const struct fb_image *image)
934{
935 struct ufx_data *dev = info->par;
936
937 sys_imageblit(info, image);
938
939 ufx_handle_damage(dev, image->dx, image->dy,
940 image->width, image->height);
941}
942
943static void ufx_ops_fillrect(struct fb_info *info,
944 const struct fb_fillrect *rect)
945{
946 struct ufx_data *dev = info->par;
947
948 sys_fillrect(info, rect);
949
950 ufx_handle_damage(dev, rect->dx, rect->dy, rect->width,
951 rect->height);
952}
953
954
955
956
957
958static void ufx_dpy_deferred_io(struct fb_info *info,
959 struct list_head *pagelist)
960{
961 struct page *cur;
962 struct fb_deferred_io *fbdefio = info->fbdefio;
963 struct ufx_data *dev = info->par;
964
965 if (!fb_defio)
966 return;
967
968 if (!atomic_read(&dev->usb_active))
969 return;
970
971
972 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
973
974
975 const int x = 0;
976 const int width = dev->info->var.xres;
977 const int y = (cur->index << PAGE_SHIFT) / (width * 2);
978 int height = (PAGE_SIZE / (width * 2)) + 1;
979 height = min(height, (int)(dev->info->var.yres - y));
980
981 BUG_ON(y >= dev->info->var.yres);
982 BUG_ON((y + height) > dev->info->var.yres);
983
984 ufx_handle_damage(dev, x, y, width, height);
985 }
986}
987
988static int ufx_ops_ioctl(struct fb_info *info, unsigned int cmd,
989 unsigned long arg)
990{
991 struct ufx_data *dev = info->par;
992 struct dloarea *area = NULL;
993
994 if (!atomic_read(&dev->usb_active))
995 return 0;
996
997
998 if (cmd == UFX_IOCTL_RETURN_EDID) {
999 u8 __user *edid = (u8 __user *)arg;
1000 if (copy_to_user(edid, dev->edid, dev->edid_size))
1001 return -EFAULT;
1002 return 0;
1003 }
1004
1005
1006 if (cmd == UFX_IOCTL_REPORT_DAMAGE) {
1007
1008
1009
1010
1011
1012
1013 if (info->fbdefio)
1014 info->fbdefio->delay = UFX_DEFIO_WRITE_DISABLE;
1015
1016 area = (struct dloarea *)arg;
1017
1018 if (area->x < 0)
1019 area->x = 0;
1020
1021 if (area->x > info->var.xres)
1022 area->x = info->var.xres;
1023
1024 if (area->y < 0)
1025 area->y = 0;
1026
1027 if (area->y > info->var.yres)
1028 area->y = info->var.yres;
1029
1030 ufx_handle_damage(dev, area->x, area->y, area->w, area->h);
1031 }
1032
1033 return 0;
1034}
1035
1036
1037static int
1038ufx_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
1039 unsigned blue, unsigned transp, struct fb_info *info)
1040{
1041 int err = 0;
1042
1043 if (regno >= info->cmap.len)
1044 return 1;
1045
1046 if (regno < 16) {
1047 if (info->var.red.offset == 10) {
1048
1049 ((u32 *) (info->pseudo_palette))[regno] =
1050 ((red & 0xf800) >> 1) |
1051 ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
1052 } else {
1053
1054 ((u32 *) (info->pseudo_palette))[regno] =
1055 ((red & 0xf800)) |
1056 ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
1057 }
1058 }
1059
1060 return err;
1061}
1062
1063
1064
1065
1066static int ufx_ops_open(struct fb_info *info, int user)
1067{
1068 struct ufx_data *dev = info->par;
1069
1070
1071
1072
1073 if (user == 0 && !console)
1074 return -EBUSY;
1075
1076
1077 if (dev->virtualized)
1078 return -ENODEV;
1079
1080 dev->fb_count++;
1081
1082 kref_get(&dev->kref);
1083
1084 if (fb_defio && (info->fbdefio == NULL)) {
1085
1086
1087 struct fb_deferred_io *fbdefio;
1088
1089 fbdefio = kzalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
1090
1091 if (fbdefio) {
1092 fbdefio->delay = UFX_DEFIO_WRITE_DELAY;
1093 fbdefio->deferred_io = ufx_dpy_deferred_io;
1094 }
1095
1096 info->fbdefio = fbdefio;
1097 fb_deferred_io_init(info);
1098 }
1099
1100 pr_debug("open /dev/fb%d user=%d fb_info=%p count=%d",
1101 info->node, user, info, dev->fb_count);
1102
1103 return 0;
1104}
1105
1106
1107
1108
1109
1110
1111static void ufx_free(struct kref *kref)
1112{
1113 struct ufx_data *dev = container_of(kref, struct ufx_data, kref);
1114
1115
1116 if (dev->urbs.count > 0)
1117 ufx_free_urb_list(dev);
1118
1119 pr_debug("freeing ufx_data %p", dev);
1120
1121 kfree(dev);
1122}
1123
1124static void ufx_release_urb_work(struct work_struct *work)
1125{
1126 struct urb_node *unode = container_of(work, struct urb_node,
1127 release_urb_work.work);
1128
1129 up(&unode->dev->urbs.limit_sem);
1130}
1131
1132static void ufx_free_framebuffer_work(struct work_struct *work)
1133{
1134 struct ufx_data *dev = container_of(work, struct ufx_data,
1135 free_framebuffer_work.work);
1136 struct fb_info *info = dev->info;
1137 int node = info->node;
1138
1139 unregister_framebuffer(info);
1140
1141 if (info->cmap.len != 0)
1142 fb_dealloc_cmap(&info->cmap);
1143 if (info->monspecs.modedb)
1144 fb_destroy_modedb(info->monspecs.modedb);
1145 vfree(info->screen_base);
1146
1147 fb_destroy_modelist(&info->modelist);
1148
1149 dev->info = NULL;
1150
1151
1152 framebuffer_release(info);
1153
1154 pr_debug("fb_info for /dev/fb%d has been freed", node);
1155
1156
1157 kref_put(&dev->kref, ufx_free);
1158}
1159
1160
1161
1162
1163static int ufx_ops_release(struct fb_info *info, int user)
1164{
1165 struct ufx_data *dev = info->par;
1166
1167 dev->fb_count--;
1168
1169
1170 if (dev->virtualized && (dev->fb_count == 0))
1171 schedule_delayed_work(&dev->free_framebuffer_work, HZ);
1172
1173 if ((dev->fb_count == 0) && (info->fbdefio)) {
1174 fb_deferred_io_cleanup(info);
1175 kfree(info->fbdefio);
1176 info->fbdefio = NULL;
1177 info->fbops->fb_mmap = ufx_ops_mmap;
1178 }
1179
1180 pr_debug("released /dev/fb%d user=%d count=%d",
1181 info->node, user, dev->fb_count);
1182
1183 kref_put(&dev->kref, ufx_free);
1184
1185 return 0;
1186}
1187
1188
1189
1190static int ufx_is_valid_mode(struct fb_videomode *mode,
1191 struct fb_info *info)
1192{
1193 if ((mode->xres * mode->yres) > (2048 * 1152)) {
1194 pr_debug("%dx%d too many pixels",
1195 mode->xres, mode->yres);
1196 return 0;
1197 }
1198
1199 if (mode->pixclock < 5000) {
1200 pr_debug("%dx%d %dps pixel clock too fast",
1201 mode->xres, mode->yres, mode->pixclock);
1202 return 0;
1203 }
1204
1205 pr_debug("%dx%d (pixclk %dps %dMHz) valid mode", mode->xres, mode->yres,
1206 mode->pixclock, (1000000 / mode->pixclock));
1207 return 1;
1208}
1209
1210static void ufx_var_color_format(struct fb_var_screeninfo *var)
1211{
1212 const struct fb_bitfield red = { 11, 5, 0 };
1213 const struct fb_bitfield green = { 5, 6, 0 };
1214 const struct fb_bitfield blue = { 0, 5, 0 };
1215
1216 var->bits_per_pixel = 16;
1217 var->red = red;
1218 var->green = green;
1219 var->blue = blue;
1220}
1221
1222static int ufx_ops_check_var(struct fb_var_screeninfo *var,
1223 struct fb_info *info)
1224{
1225 struct fb_videomode mode;
1226
1227
1228 if ((var->xres * var->yres * 2) > info->fix.smem_len)
1229 return -EINVAL;
1230
1231
1232 ufx_var_color_format(var);
1233
1234 fb_var_to_videomode(&mode, var);
1235
1236 if (!ufx_is_valid_mode(&mode, info))
1237 return -EINVAL;
1238
1239 return 0;
1240}
1241
1242static int ufx_ops_set_par(struct fb_info *info)
1243{
1244 struct ufx_data *dev = info->par;
1245 int result;
1246 u16 *pix_framebuffer;
1247 int i;
1248
1249 pr_debug("set_par mode %dx%d", info->var.xres, info->var.yres);
1250 result = ufx_set_vid_mode(dev, &info->var);
1251
1252 if ((result == 0) && (dev->fb_count == 0)) {
1253
1254 pix_framebuffer = (u16 *) info->screen_base;
1255 for (i = 0; i < info->fix.smem_len / 2; i++)
1256 pix_framebuffer[i] = 0x37e6;
1257
1258 ufx_handle_damage(dev, 0, 0, info->var.xres, info->var.yres);
1259 }
1260
1261
1262 if (info->fbdefio)
1263 info->fbdefio->delay = UFX_DEFIO_WRITE_DELAY;
1264
1265 return result;
1266}
1267
1268
1269static int ufx_ops_blank(int blank_mode, struct fb_info *info)
1270{
1271 struct ufx_data *dev = info->par;
1272 ufx_set_vid_mode(dev, &info->var);
1273 return 0;
1274}
1275
1276static struct fb_ops ufx_ops = {
1277 .owner = THIS_MODULE,
1278 .fb_read = fb_sys_read,
1279 .fb_write = ufx_ops_write,
1280 .fb_setcolreg = ufx_ops_setcolreg,
1281 .fb_fillrect = ufx_ops_fillrect,
1282 .fb_copyarea = ufx_ops_copyarea,
1283 .fb_imageblit = ufx_ops_imageblit,
1284 .fb_mmap = ufx_ops_mmap,
1285 .fb_ioctl = ufx_ops_ioctl,
1286 .fb_open = ufx_ops_open,
1287 .fb_release = ufx_ops_release,
1288 .fb_blank = ufx_ops_blank,
1289 .fb_check_var = ufx_ops_check_var,
1290 .fb_set_par = ufx_ops_set_par,
1291};
1292
1293
1294
1295static int ufx_realloc_framebuffer(struct ufx_data *dev, struct fb_info *info)
1296{
1297 int retval = -ENOMEM;
1298 int old_len = info->fix.smem_len;
1299 int new_len;
1300 unsigned char *old_fb = info->screen_base;
1301 unsigned char *new_fb;
1302
1303 pr_debug("Reallocating framebuffer. Addresses will change!");
1304
1305 new_len = info->fix.line_length * info->var.yres;
1306
1307 if (PAGE_ALIGN(new_len) > old_len) {
1308
1309
1310
1311 new_fb = vmalloc(new_len);
1312 if (!new_fb) {
1313 pr_err("Virtual framebuffer alloc failed");
1314 goto error;
1315 }
1316
1317 if (info->screen_base) {
1318 memcpy(new_fb, old_fb, old_len);
1319 vfree(info->screen_base);
1320 }
1321
1322 info->screen_base = new_fb;
1323 info->fix.smem_len = PAGE_ALIGN(new_len);
1324 info->fix.smem_start = (unsigned long) new_fb;
1325 info->flags = smscufx_info_flags;
1326 }
1327
1328 retval = 0;
1329
1330error:
1331 return retval;
1332}
1333
1334
1335
1336static int ufx_i2c_init(struct ufx_data *dev)
1337{
1338 u32 tmp;
1339
1340
1341 int status = ufx_reg_write(dev, 0x106C, 0x00);
1342 check_warn_return(status, "failed to disable I2C");
1343
1344
1345
1346 status = ufx_reg_write(dev, 0x1018, 12);
1347 check_warn_return(status, "error writing 0x1018");
1348
1349
1350 status = ufx_reg_write(dev, 0x1014, 6);
1351 check_warn_return(status, "error writing 0x1014");
1352
1353 status = ufx_reg_read(dev, 0x1000, &tmp);
1354 check_warn_return(status, "error reading 0x1000");
1355
1356
1357 tmp &= ~(0x06);
1358 tmp |= 0x02;
1359
1360
1361 tmp &= ~(0x10);
1362
1363
1364 tmp |= 0x21;
1365
1366 status = ufx_reg_write(dev, 0x1000, tmp);
1367 check_warn_return(status, "error writing 0x1000");
1368
1369
1370 status = ufx_reg_clear_and_set_bits(dev, 0x1004, 0xC00, 0x000);
1371 check_warn_return(status, "error setting TX mode bits in 0x1004");
1372
1373
1374 status = ufx_reg_write(dev, 0x106C, 0x01);
1375 check_warn_return(status, "failed to enable I2C");
1376
1377 return 0;
1378}
1379
1380
1381static int ufx_i2c_configure(struct ufx_data *dev)
1382{
1383 int status = ufx_reg_write(dev, 0x106C, 0x00);
1384 check_warn_return(status, "failed to disable I2C");
1385
1386 status = ufx_reg_write(dev, 0x3010, 0x00000000);
1387 check_warn_return(status, "failed to write 0x3010");
1388
1389
1390 status = ufx_reg_clear_and_set_bits(dev, 0x1004, 0x3FF, (0xA0 >> 1));
1391 check_warn_return(status, "failed to set TAR bits in 0x1004");
1392
1393 status = ufx_reg_write(dev, 0x106C, 0x01);
1394 check_warn_return(status, "failed to enable I2C");
1395
1396 return 0;
1397}
1398
1399
1400
1401static int ufx_i2c_wait_busy(struct ufx_data *dev)
1402{
1403 u32 tmp;
1404 int i, status;
1405
1406 for (i = 0; i < 15; i++) {
1407 status = ufx_reg_read(dev, 0x1100, &tmp);
1408 check_warn_return(status, "0x1100 read failed");
1409
1410
1411 if ((tmp & 0x80000000) == 0) {
1412 if (tmp & 0x20000000) {
1413 pr_warn("I2C read failed, 0x1100=0x%08x", tmp);
1414 return -EIO;
1415 }
1416
1417 return 0;
1418 }
1419
1420
1421 if (i >= 10)
1422 msleep(10);
1423 }
1424
1425 pr_warn("I2C access timed out, resetting I2C hardware");
1426 status = ufx_reg_write(dev, 0x1100, 0x40000000);
1427 check_warn_return(status, "0x1100 write failed");
1428
1429 return -ETIMEDOUT;
1430}
1431
1432
1433static int ufx_read_edid(struct ufx_data *dev, u8 *edid, int edid_len)
1434{
1435 int i, j, status;
1436 u32 *edid_u32 = (u32 *)edid;
1437
1438 BUG_ON(edid_len != EDID_LENGTH);
1439
1440 status = ufx_i2c_configure(dev);
1441 if (status < 0) {
1442 pr_err("ufx_i2c_configure failed");
1443 return status;
1444 }
1445
1446 memset(edid, 0xff, EDID_LENGTH);
1447
1448
1449 for (i = 0; i < 2; i++) {
1450 u32 temp = 0x28070000 | (63 << 20) | (((u32)(i * 64)) << 8);
1451 status = ufx_reg_write(dev, 0x1100, temp);
1452 check_warn_return(status, "Failed to write 0x1100");
1453
1454 temp |= 0x80000000;
1455 status = ufx_reg_write(dev, 0x1100, temp);
1456 check_warn_return(status, "Failed to write 0x1100");
1457
1458 status = ufx_i2c_wait_busy(dev);
1459 check_warn_return(status, "Timeout waiting for I2C BUSY to clear");
1460
1461 for (j = 0; j < 16; j++) {
1462 u32 data_reg_addr = 0x1110 + (j * 4);
1463 status = ufx_reg_read(dev, data_reg_addr, edid_u32++);
1464 check_warn_return(status, "Error reading i2c data");
1465 }
1466 }
1467
1468
1469 for (i = 0; i < 16; i++) {
1470 if (edid[i] != 0xFF) {
1471 pr_debug("edid data read successfully");
1472 return EDID_LENGTH;
1473 }
1474 }
1475
1476 pr_warn("edid data contains all 0xff");
1477 return -ETIMEDOUT;
1478}
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492static int ufx_setup_modes(struct ufx_data *dev, struct fb_info *info,
1493 char *default_edid, size_t default_edid_size)
1494{
1495 const struct fb_videomode *default_vmode = NULL;
1496 u8 *edid;
1497 int i, result = 0, tries = 3;
1498
1499 if (info->dev)
1500 mutex_lock(&info->lock);
1501
1502 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
1503 if (!edid) {
1504 result = -ENOMEM;
1505 goto error;
1506 }
1507
1508 fb_destroy_modelist(&info->modelist);
1509 memset(&info->monspecs, 0, sizeof(info->monspecs));
1510
1511
1512
1513
1514 while (tries--) {
1515 i = ufx_read_edid(dev, edid, EDID_LENGTH);
1516
1517 if (i >= EDID_LENGTH)
1518 fb_edid_to_monspecs(edid, &info->monspecs);
1519
1520 if (info->monspecs.modedb_len > 0) {
1521 dev->edid = edid;
1522 dev->edid_size = i;
1523 break;
1524 }
1525 }
1526
1527
1528 if (info->monspecs.modedb_len == 0) {
1529 pr_err("Unable to get valid EDID from device/display\n");
1530
1531 if (dev->edid) {
1532 fb_edid_to_monspecs(dev->edid, &info->monspecs);
1533 if (info->monspecs.modedb_len > 0)
1534 pr_err("Using previously queried EDID\n");
1535 }
1536 }
1537
1538
1539 if (info->monspecs.modedb_len == 0) {
1540 if (default_edid_size >= EDID_LENGTH) {
1541 fb_edid_to_monspecs(default_edid, &info->monspecs);
1542 if (info->monspecs.modedb_len > 0) {
1543 memcpy(edid, default_edid, default_edid_size);
1544 dev->edid = edid;
1545 dev->edid_size = default_edid_size;
1546 pr_err("Using default/backup EDID\n");
1547 }
1548 }
1549 }
1550
1551
1552 if (info->monspecs.modedb_len > 0) {
1553
1554 for (i = 0; i < info->monspecs.modedb_len; i++) {
1555 if (ufx_is_valid_mode(&info->monspecs.modedb[i], info))
1556 fb_add_videomode(&info->monspecs.modedb[i],
1557 &info->modelist);
1558 else
1559 info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
1560 }
1561
1562 default_vmode = fb_find_best_display(&info->monspecs,
1563 &info->modelist);
1564 }
1565
1566
1567 if (default_vmode == NULL) {
1568
1569 struct fb_videomode fb_vmode = {0};
1570
1571
1572
1573
1574
1575
1576 for (i = 0; i < VESA_MODEDB_SIZE; i++) {
1577 if (ufx_is_valid_mode((struct fb_videomode *)
1578 &vesa_modes[i], info))
1579 fb_add_videomode(&vesa_modes[i],
1580 &info->modelist);
1581 }
1582
1583
1584
1585
1586 fb_vmode.xres = 800;
1587 fb_vmode.yres = 600;
1588 fb_vmode.refresh = 60;
1589 default_vmode = fb_find_nearest_mode(&fb_vmode,
1590 &info->modelist);
1591 }
1592
1593
1594 if ((default_vmode != NULL) && (dev->fb_count == 0)) {
1595
1596 fb_videomode_to_var(&info->var, default_vmode);
1597 ufx_var_color_format(&info->var);
1598
1599
1600 memcpy(&info->fix, &ufx_fix, sizeof(ufx_fix));
1601 info->fix.line_length = info->var.xres *
1602 (info->var.bits_per_pixel / 8);
1603
1604 result = ufx_realloc_framebuffer(dev, info);
1605
1606 } else
1607 result = -EINVAL;
1608
1609error:
1610 if (edid && (dev->edid != edid))
1611 kfree(edid);
1612
1613 if (info->dev)
1614 mutex_unlock(&info->lock);
1615
1616 return result;
1617}
1618
1619static int ufx_usb_probe(struct usb_interface *interface,
1620 const struct usb_device_id *id)
1621{
1622 struct usb_device *usbdev;
1623 struct ufx_data *dev;
1624 struct fb_info *info = NULL;
1625 int retval = -ENOMEM;
1626 u32 id_rev, fpga_rev;
1627
1628
1629 usbdev = interface_to_usbdev(interface);
1630 BUG_ON(!usbdev);
1631
1632 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1633 if (dev == NULL) {
1634 dev_err(&usbdev->dev, "ufx_usb_probe: failed alloc of dev struct\n");
1635 goto error;
1636 }
1637
1638
1639 kref_init(&dev->kref);
1640 kref_get(&dev->kref);
1641
1642 dev->udev = usbdev;
1643 dev->gdev = &usbdev->dev;
1644 usb_set_intfdata(interface, dev);
1645
1646 dev_dbg(dev->gdev, "%s %s - serial #%s\n",
1647 usbdev->manufacturer, usbdev->product, usbdev->serial);
1648 dev_dbg(dev->gdev, "vid_%04x&pid_%04x&rev_%04x driver's ufx_data struct at %p\n",
1649 usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
1650 usbdev->descriptor.bcdDevice, dev);
1651 dev_dbg(dev->gdev, "console enable=%d\n", console);
1652 dev_dbg(dev->gdev, "fb_defio enable=%d\n", fb_defio);
1653
1654 if (!ufx_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
1655 retval = -ENOMEM;
1656 dev_err(dev->gdev, "ufx_alloc_urb_list failed\n");
1657 goto error;
1658 }
1659
1660
1661
1662
1663 info = framebuffer_alloc(0, &usbdev->dev);
1664 if (!info) {
1665 retval = -ENOMEM;
1666 dev_err(dev->gdev, "framebuffer_alloc failed\n");
1667 goto error;
1668 }
1669
1670 dev->info = info;
1671 info->par = dev;
1672 info->pseudo_palette = dev->pseudo_palette;
1673 info->fbops = &ufx_ops;
1674
1675 retval = fb_alloc_cmap(&info->cmap, 256, 0);
1676 if (retval < 0) {
1677 dev_err(dev->gdev, "fb_alloc_cmap failed %x\n", retval);
1678 goto error;
1679 }
1680
1681 INIT_DELAYED_WORK(&dev->free_framebuffer_work,
1682 ufx_free_framebuffer_work);
1683
1684 INIT_LIST_HEAD(&info->modelist);
1685
1686 retval = ufx_reg_read(dev, 0x3000, &id_rev);
1687 check_warn_goto_error(retval, "error %d reading 0x3000 register from device", retval);
1688 dev_dbg(dev->gdev, "ID_REV register value 0x%08x", id_rev);
1689
1690 retval = ufx_reg_read(dev, 0x3004, &fpga_rev);
1691 check_warn_goto_error(retval, "error %d reading 0x3004 register from device", retval);
1692 dev_dbg(dev->gdev, "FPGA_REV register value 0x%08x", fpga_rev);
1693
1694 dev_dbg(dev->gdev, "resetting device");
1695 retval = ufx_lite_reset(dev);
1696 check_warn_goto_error(retval, "error %d resetting device", retval);
1697
1698 dev_dbg(dev->gdev, "configuring system clock");
1699 retval = ufx_config_sys_clk(dev);
1700 check_warn_goto_error(retval, "error %d configuring system clock", retval);
1701
1702 dev_dbg(dev->gdev, "configuring DDR2 controller");
1703 retval = ufx_config_ddr2(dev);
1704 check_warn_goto_error(retval, "error %d initialising DDR2 controller", retval);
1705
1706 dev_dbg(dev->gdev, "configuring I2C controller");
1707 retval = ufx_i2c_init(dev);
1708 check_warn_goto_error(retval, "error %d initialising I2C controller", retval);
1709
1710 dev_dbg(dev->gdev, "selecting display mode");
1711 retval = ufx_setup_modes(dev, info, NULL, 0);
1712 check_warn_goto_error(retval, "unable to find common mode for display and adapter");
1713
1714 retval = ufx_reg_set_bits(dev, 0x4000, 0x00000001);
1715 check_warn_goto_error(retval, "error %d enabling graphics engine", retval);
1716
1717
1718 atomic_set(&dev->usb_active, 1);
1719
1720 dev_dbg(dev->gdev, "checking var");
1721 retval = ufx_ops_check_var(&info->var, info);
1722 check_warn_goto_error(retval, "error %d ufx_ops_check_var", retval);
1723
1724 dev_dbg(dev->gdev, "setting par");
1725 retval = ufx_ops_set_par(info);
1726 check_warn_goto_error(retval, "error %d ufx_ops_set_par", retval);
1727
1728 dev_dbg(dev->gdev, "registering framebuffer");
1729 retval = register_framebuffer(info);
1730 check_warn_goto_error(retval, "error %d register_framebuffer", retval);
1731
1732 dev_info(dev->gdev, "SMSC UDX USB device /dev/fb%d attached. %dx%d resolution."
1733 " Using %dK framebuffer memory\n", info->node,
1734 info->var.xres, info->var.yres, info->fix.smem_len >> 10);
1735
1736 return 0;
1737
1738error:
1739 if (dev) {
1740 if (info) {
1741 if (info->cmap.len != 0)
1742 fb_dealloc_cmap(&info->cmap);
1743 if (info->monspecs.modedb)
1744 fb_destroy_modedb(info->monspecs.modedb);
1745 vfree(info->screen_base);
1746
1747 fb_destroy_modelist(&info->modelist);
1748
1749 framebuffer_release(info);
1750 }
1751
1752 kref_put(&dev->kref, ufx_free);
1753 kref_put(&dev->kref, ufx_free);
1754
1755
1756 }
1757
1758 return retval;
1759}
1760
1761static void ufx_usb_disconnect(struct usb_interface *interface)
1762{
1763 struct ufx_data *dev;
1764 struct fb_info *info;
1765
1766 dev = usb_get_intfdata(interface);
1767 info = dev->info;
1768
1769 pr_debug("USB disconnect starting\n");
1770
1771
1772 dev->virtualized = true;
1773
1774
1775 atomic_set(&dev->usb_active, 0);
1776
1777 usb_set_intfdata(interface, NULL);
1778
1779
1780 if (dev->fb_count == 0)
1781 schedule_delayed_work(&dev->free_framebuffer_work, 0);
1782
1783
1784 kref_put(&dev->kref, ufx_free);
1785
1786
1787}
1788
1789static struct usb_driver ufx_driver = {
1790 .name = "smscufx",
1791 .probe = ufx_usb_probe,
1792 .disconnect = ufx_usb_disconnect,
1793 .id_table = id_table,
1794};
1795
1796module_usb_driver(ufx_driver);
1797
1798static void ufx_urb_completion(struct urb *urb)
1799{
1800 struct urb_node *unode = urb->context;
1801 struct ufx_data *dev = unode->dev;
1802 unsigned long flags;
1803
1804
1805 if (urb->status) {
1806 if (!(urb->status == -ENOENT ||
1807 urb->status == -ECONNRESET ||
1808 urb->status == -ESHUTDOWN)) {
1809 pr_err("%s - nonzero write bulk status received: %d\n",
1810 __func__, urb->status);
1811 atomic_set(&dev->lost_pixels, 1);
1812 }
1813 }
1814
1815 urb->transfer_buffer_length = dev->urbs.size;
1816
1817 spin_lock_irqsave(&dev->urbs.lock, flags);
1818 list_add_tail(&unode->entry, &dev->urbs.list);
1819 dev->urbs.available++;
1820 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1821
1822
1823
1824 if (fb_defio)
1825 schedule_delayed_work(&unode->release_urb_work, 0);
1826 else
1827 up(&dev->urbs.limit_sem);
1828}
1829
1830static void ufx_free_urb_list(struct ufx_data *dev)
1831{
1832 int count = dev->urbs.count;
1833 struct list_head *node;
1834 struct urb_node *unode;
1835 struct urb *urb;
1836 int ret;
1837 unsigned long flags;
1838
1839 pr_debug("Waiting for completes and freeing all render urbs\n");
1840
1841
1842 while (count--) {
1843
1844 ret = down_interruptible(&dev->urbs.limit_sem);
1845 if (ret)
1846 break;
1847
1848 spin_lock_irqsave(&dev->urbs.lock, flags);
1849
1850 node = dev->urbs.list.next;
1851 list_del_init(node);
1852
1853 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1854
1855 unode = list_entry(node, struct urb_node, entry);
1856 urb = unode->urb;
1857
1858
1859 usb_free_coherent(urb->dev, dev->urbs.size,
1860 urb->transfer_buffer, urb->transfer_dma);
1861 usb_free_urb(urb);
1862 kfree(node);
1863 }
1864}
1865
1866static int ufx_alloc_urb_list(struct ufx_data *dev, int count, size_t size)
1867{
1868 int i = 0;
1869 struct urb *urb;
1870 struct urb_node *unode;
1871 char *buf;
1872
1873 spin_lock_init(&dev->urbs.lock);
1874
1875 dev->urbs.size = size;
1876 INIT_LIST_HEAD(&dev->urbs.list);
1877
1878 while (i < count) {
1879 unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
1880 if (!unode)
1881 break;
1882 unode->dev = dev;
1883
1884 INIT_DELAYED_WORK(&unode->release_urb_work,
1885 ufx_release_urb_work);
1886
1887 urb = usb_alloc_urb(0, GFP_KERNEL);
1888 if (!urb) {
1889 kfree(unode);
1890 break;
1891 }
1892 unode->urb = urb;
1893
1894 buf = usb_alloc_coherent(dev->udev, size, GFP_KERNEL,
1895 &urb->transfer_dma);
1896 if (!buf) {
1897 kfree(unode);
1898 usb_free_urb(urb);
1899 break;
1900 }
1901
1902
1903 usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
1904 buf, size, ufx_urb_completion, unode);
1905 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1906
1907 list_add_tail(&unode->entry, &dev->urbs.list);
1908
1909 i++;
1910 }
1911
1912 sema_init(&dev->urbs.limit_sem, i);
1913 dev->urbs.count = i;
1914 dev->urbs.available = i;
1915
1916 pr_debug("allocated %d %d byte urbs\n", i, (int) size);
1917
1918 return i;
1919}
1920
1921static struct urb *ufx_get_urb(struct ufx_data *dev)
1922{
1923 int ret = 0;
1924 struct list_head *entry;
1925 struct urb_node *unode;
1926 struct urb *urb = NULL;
1927 unsigned long flags;
1928
1929
1930 ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
1931 if (ret) {
1932 atomic_set(&dev->lost_pixels, 1);
1933 pr_warn("wait for urb interrupted: %x available: %d\n",
1934 ret, dev->urbs.available);
1935 goto error;
1936 }
1937
1938 spin_lock_irqsave(&dev->urbs.lock, flags);
1939
1940 BUG_ON(list_empty(&dev->urbs.list));
1941 entry = dev->urbs.list.next;
1942 list_del_init(entry);
1943 dev->urbs.available--;
1944
1945 spin_unlock_irqrestore(&dev->urbs.lock, flags);
1946
1947 unode = list_entry(entry, struct urb_node, entry);
1948 urb = unode->urb;
1949
1950error:
1951 return urb;
1952}
1953
1954static int ufx_submit_urb(struct ufx_data *dev, struct urb *urb, size_t len)
1955{
1956 int ret;
1957
1958 BUG_ON(len > dev->urbs.size);
1959
1960 urb->transfer_buffer_length = len;
1961 ret = usb_submit_urb(urb, GFP_KERNEL);
1962 if (ret) {
1963 ufx_urb_completion(urb);
1964 atomic_set(&dev->lost_pixels, 1);
1965 pr_err("usb_submit_urb error %x\n", ret);
1966 }
1967 return ret;
1968}
1969
1970module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1971MODULE_PARM_DESC(console, "Allow fbcon to be used on this display");
1972
1973module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
1974MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support");
1975
1976MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
1977MODULE_DESCRIPTION("SMSC UFX kernel framebuffer driver");
1978MODULE_LICENSE("GPL");
1979