summaryrefslogtreecommitdiff
path: root/board/freescale/common/vsc3316_3308.c
blob: 8aceb8ef178726f715e01c9b7767ceabdfdba5c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright 2012 Freescale Semiconductor, Inc.
 * Copyright 2020 NXP
 */

#include "vsc3316_3308.h"
#include <log.h>

#define REVISION_ID_REG		0x7E
#define INTERFACE_MODE_REG		0x79
#define CURRENT_PAGE_REGISTER		0x7F
#define CONNECTION_CONFIG_PAGE		0x00
#define INPUT_STATE_REG		0x13
#define GLOBAL_INPUT_ISE1		0x51
#define GLOBAL_INPUT_ISE2		0x52
#define GLOBAL_INPUT_GAIN		0x53
#define GLOBAL_INPUT_LOS		0x55
#define GLOBAL_OUTPUT_PE1		0x56
#define GLOBAL_OUTPUT_PE2		0x57
#define GLOBAL_OUTPUT_LEVEL		0x58
#define GLOBAL_OUTPUT_TERMINATION	0x5A
#define GLOBAL_CORE_CNTRL		0x5D
#define OUTPUT_MODE_PAGE		0x23
#define CORE_CONTROL_PAGE		0x25
#define CORE_CONFIG_REG		0x75

int vsc_if_enable(unsigned int vsc_addr)
{
	u8 data;

	debug("VSC:Configuring VSC at I2C address 0x%2x"
			" for 2-wire interface\n", vsc_addr);

	/* enable 2-wire Serial InterFace (I2C) */
	data = 0x02;
#ifdef CONFIG_DM_I2C
	int ret, bus_num = 0;
	struct udevice *dev;

	ret = i2c_get_chip_for_busnum(bus_num, vsc_addr,
				      1, &dev);
	if (ret) {
		printf("%s: Cannot find udev for a bus %d\n", __func__,
		       bus_num);
		return ret;
	}

	return dm_i2c_write(dev, INTERFACE_MODE_REG, &data, 1);
#else
	return i2c_write(vsc_addr, INTERFACE_MODE_REG, 1, &data, 1);
#endif
}

int vsc3316_config(unsigned int vsc_addr, int8_t con_arr[][2],
		unsigned int num_con)
{
	unsigned int i;
	u8 rev_id = 0;
	int ret;

	debug("VSC:Initializing VSC3316 at I2C address 0x%2x"
		" for Tx\n", vsc_addr);

#ifdef CONFIG_DM_I2C
	int bus_num = 0;
	struct udevice *dev;

	ret = i2c_get_chip_for_busnum(bus_num, vsc_addr,
				      1, &dev);
	if (ret) {
		printf("%s: Cannot find udev for a bus %d\n", __func__,
		       bus_num);
		return ret;
	}

	ret = dm_i2c_read(dev, REVISION_ID_REG, &rev_id, 1);
	if (ret < 0) {
		printf("VSC:0x%x could not read REV_ID from device.\n",
		       vsc_addr);
		return ret;
	}

	if (rev_id != 0xab) {
		printf("VSC: device at address 0x%x is not VSC3316/3308.\n",
		       vsc_addr);
		return -ENODEV;
	}

	ret = vsc_if_enable(vsc_addr);
	if (ret) {
		printf("VSC:0x%x could not configured for 2-wire I/F.\n",
		       vsc_addr);
		return ret;
	}

	/* config connections - page 0x00 */
	dm_i2c_reg_write(dev, CURRENT_PAGE_REGISTER, CONNECTION_CONFIG_PAGE);

	/* Making crosspoint connections, by connecting required
	 * input to output
	 */
	for (i = 0; i < num_con ; i++)
		dm_i2c_reg_write(dev, con_arr[i][1], con_arr[i][0]);

	/* input state - page 0x13 */
	dm_i2c_reg_write(dev, CURRENT_PAGE_REGISTER, INPUT_STATE_REG);
	/* Configuring the required input of the switch */
	for (i = 0; i < num_con ; i++)
		dm_i2c_reg_write(dev, con_arr[i][0], 0x80);

	/* Setting Global Input LOS threshold value */
	dm_i2c_reg_write(dev, GLOBAL_INPUT_LOS, 0x60);

	/* config output mode - page 0x23 */
	dm_i2c_reg_write(dev, CURRENT_PAGE_REGISTER, OUTPUT_MODE_PAGE);
	/* Turn ON the Output driver correspond to required output*/
	for (i = 0; i < num_con ; i++)
		dm_i2c_reg_write(dev,  con_arr[i][1], 0);

	/* configure global core control register, Turn on Global core power */
	dm_i2c_reg_write(dev, GLOBAL_CORE_CNTRL, 0);

#else
	ret = i2c_read(vsc_addr, REVISION_ID_REG, 1, &rev_id, 1);
	if (ret < 0) {
		printf("VSC:0x%x could not read REV_ID from device.\n",
			vsc_addr);
		return ret;
	}

	if (rev_id != 0xab) {
		printf("VSC: device at address 0x%x is not VSC3316/3308.\n",
			vsc_addr);
		return -ENODEV;
	}

	ret = vsc_if_enable(vsc_addr);
	if (ret) {
		printf("VSC:0x%x could not configured for 2-wire I/F.\n",
			vsc_addr);
		return ret;
	}

	/* config connections - page 0x00 */
	i2c_reg_write(vsc_addr, CURRENT_PAGE_REGISTER, CONNECTION_CONFIG_PAGE);

	/* Making crosspoint connections, by connecting required
	 * input to output */
	for (i = 0; i < num_con ; i++)
		i2c_reg_write(vsc_addr, con_arr[i][1], con_arr[i][0]);

	/* input state - page 0x13 */
	i2c_reg_write(vsc_addr, CURRENT_PAGE_REGISTER, INPUT_STATE_REG);
	/* Configuring the required input of the switch */
	for (i = 0; i < num_con ; i++)
		i2c_reg_write(vsc_addr, con_arr[i][0], 0x80);

	/* Setting Global Input LOS threshold value */
	i2c_reg_write(vsc_addr, GLOBAL_INPUT_LOS, 0x60);

	/* config output mode - page 0x23 */
	i2c_reg_write(vsc_addr, CURRENT_PAGE_REGISTER, OUTPUT_MODE_PAGE);
	/* Turn ON the Output driver correspond to required output*/
	for (i = 0; i < num_con ; i++)
		i2c_reg_write(vsc_addr,  con_arr[i][1], 0);

	/* configure global core control register, Turn on Global core power */
	i2c_reg_write(vsc_addr, GLOBAL_CORE_CNTRL, 0);
#endif

	vsc_wp_config(vsc_addr);

	return 0;
}

#ifdef CONFIG_SYS_FSL_B4860QDS_XFI_ERR
int vsc3308_config_adjust(unsigned int vsc_addr, const int8_t con_arr[][2],
		unsigned int num_con)
{
	unsigned int i;
	u8 rev_id = 0;
	int ret;

	debug("VSC:Initializing VSC3308 at I2C address 0x%x for Tx\n",
	      vsc_addr);

#ifdef CONFIG_DM_I2C
	int bus_num = 0;
	struct udevice *dev;

	ret = i2c_get_chip_for_busnum(bus_num, vsc_addr,
				      1, &dev);
	if (ret) {
		printf("%s: Cannot find udev for a bus %d\n", __func__,
		       bus_num);
		return ret;
	}

	ret = dm_i2c_read(dev, REVISION_ID_REG, &rev_id, 1);
	if (ret < 0) {
		printf("VSC:0x%x could not read REV_ID from device.\n",
		       vsc_addr);
		return ret;
	}

	if (rev_id != 0xab) {
		printf("VSC: device at address 0x%x is not VSC3316/3308.\n",
		       vsc_addr);
		return -ENODEV;
	}

	ret = vsc_if_enable(vsc_addr);
	if (ret) {
		printf("VSC:0x%x could not configured for 2-wire I/F.\n",
		       vsc_addr);
		return ret;
	}

	/* config connections - page 0x00 */
	dm_i2c_reg_write(dev, CURRENT_PAGE_REGISTER, CONNECTION_CONFIG_PAGE);

	/* Configure Global Input ISE */
	dm_i2c_reg_write(dev, GLOBAL_INPUT_ISE1, 0);
	dm_i2c_reg_write(dev, GLOBAL_INPUT_ISE2, 0);

	/* Configure Tx/Rx Global Output PE1 */
	dm_i2c_reg_write(dev, GLOBAL_OUTPUT_PE1, 0);

	/* Configure Tx/Rx Global Output PE2 */
	dm_i2c_reg_write(dev, GLOBAL_OUTPUT_PE2, 0);

	/* Configure Tx/Rx Global Input GAIN */
	dm_i2c_reg_write(dev, GLOBAL_INPUT_GAIN, 0x3F);

	/* Setting Global Input LOS threshold value */
	dm_i2c_reg_write(dev, GLOBAL_INPUT_LOS, 0xE0);

	/* Setting Global output termination */
	dm_i2c_reg_write(dev, GLOBAL_OUTPUT_TERMINATION, 0);

	/* Configure Tx/Rx Global Output level */
	if (vsc_addr == VSC3308_TX_ADDRESS)
		dm_i2c_reg_write(dev, GLOBAL_OUTPUT_LEVEL, 4);
	else
		dm_i2c_reg_write(dev, GLOBAL_OUTPUT_LEVEL, 2);

	/* Making crosspoint connections, by connecting required
	 * input to output
	 */
	for (i = 0; i < num_con ; i++)
		dm_i2c_reg_write(dev, con_arr[i][1], con_arr[i][0]);

	/* input state - page 0x13 */
	dm_i2c_reg_write(dev, CURRENT_PAGE_REGISTER, INPUT_STATE_REG);
	/* Turning off all the required input of the switch */
	for (i = 0; i < num_con; i++)
		dm_i2c_reg_write(dev, con_arr[i][0], 1);

	/* only turn on specific Tx/Rx requested by the XFI erratum */
	if (vsc_addr == VSC3308_TX_ADDRESS) {
		dm_i2c_reg_write(dev, 2, 0);
		dm_i2c_reg_write(dev, 3, 0);
	} else {
		dm_i2c_reg_write(dev, 0, 0);
		dm_i2c_reg_write(dev, 1, 0);
	}

	/* config output mode - page 0x23 */
	dm_i2c_reg_write(dev, CURRENT_PAGE_REGISTER, OUTPUT_MODE_PAGE);
	/* Turn off the Output driver correspond to required output*/
	for (i = 0; i < num_con ; i++)
		dm_i2c_reg_write(dev,  con_arr[i][1], 1);

	/* only turn on specific Tx/Rx requested by the XFI erratum */
	if (vsc_addr == VSC3308_TX_ADDRESS) {
		dm_i2c_reg_write(dev, 0, 0);
		dm_i2c_reg_write(dev, 1, 0);
	} else {
		dm_i2c_reg_write(dev, 3, 0);
		dm_i2c_reg_write(dev, 4, 0);
	}

	/* configure global core control register, Turn on Global core power */
	dm_i2c_reg_write(dev, GLOBAL_CORE_CNTRL, 0);
#else
	ret = i2c_read(vsc_addr, REVISION_ID_REG, 1, &rev_id, 1);
	if (ret < 0) {
		printf("VSC:0x%x could not read REV_ID from device.\n",
		       vsc_addr);
		return ret;
	}

	if (rev_id != 0xab) {
		printf("VSC: device at address 0x%x is not VSC3316/3308.\n",
		       vsc_addr);
		return -ENODEV;
	}

	ret = vsc_if_enable(vsc_addr);
	if (ret) {
		printf("VSC:0x%x could not configured for 2-wire I/F.\n",
		       vsc_addr);
		return ret;
	}

	/* config connections - page 0x00 */
	i2c_reg_write(vsc_addr, CURRENT_PAGE_REGISTER, CONNECTION_CONFIG_PAGE);

	/* Configure Global Input ISE */
	i2c_reg_write(vsc_addr, GLOBAL_INPUT_ISE1, 0);
	i2c_reg_write(vsc_addr, GLOBAL_INPUT_ISE2, 0);

	/* Configure Tx/Rx Global Output PE1 */
	i2c_reg_write(vsc_addr, GLOBAL_OUTPUT_PE1, 0);

	/* Configure Tx/Rx Global Output PE2 */
	i2c_reg_write(vsc_addr, GLOBAL_OUTPUT_PE2, 0);

	/* Configure Tx/Rx Global Input GAIN */
	i2c_reg_write(vsc_addr, GLOBAL_INPUT_GAIN, 0x3F);

	/* Setting Global Input LOS threshold value */
	i2c_reg_write(vsc_addr, GLOBAL_INPUT_LOS, 0xE0);

	/* Setting Global output termination */
	i2c_reg_write(vsc_addr, GLOBAL_OUTPUT_TERMINATION, 0);

	/* Configure Tx/Rx Global Output level */
	if (vsc_addr == VSC3308_TX_ADDRESS)
		i2c_reg_write(vsc_addr, GLOBAL_OUTPUT_LEVEL, 4);
	else
		i2c_reg_write(vsc_addr, GLOBAL_OUTPUT_LEVEL, 2);

	/* Making crosspoint connections, by connecting required
	 * input to output */
	for (i = 0; i < num_con ; i++)
		i2c_reg_write(vsc_addr, con_arr[i][1], con_arr[i][0]);

	/* input state - page 0x13 */
	i2c_reg_write(vsc_addr, CURRENT_PAGE_REGISTER, INPUT_STATE_REG);
	/* Turning off all the required input of the switch */
	for (i = 0; i < num_con; i++)
		i2c_reg_write(vsc_addr, con_arr[i][0], 1);

	/* only turn on specific Tx/Rx requested by the XFI erratum */
	if (vsc_addr == VSC3308_TX_ADDRESS) {
		i2c_reg_write(vsc_addr, 2, 0);
		i2c_reg_write(vsc_addr, 3, 0);
	} else {
		i2c_reg_write(vsc_addr, 0, 0);
		i2c_reg_write(vsc_addr, 1, 0);
	}

	/* config output mode - page 0x23 */
	i2c_reg_write(vsc_addr, CURRENT_PAGE_REGISTER, OUTPUT_MODE_PAGE);
	/* Turn off the Output driver correspond to required output*/
	for (i = 0; i < num_con ; i++)
		i2c_reg_write(vsc_addr,  con_arr[i][1], 1);

	/* only turn on specific Tx/Rx requested by the XFI erratum */
	if (vsc_addr == VSC3308_TX_ADDRESS) {
		i2c_reg_write(vsc_addr, 0, 0);
		i2c_reg_write(vsc_addr, 1, 0);
	} else {
		i2c_reg_write(vsc_addr, 3, 0);
		i2c_reg_write(vsc_addr, 4, 0);
	}

	/* configure global core control register, Turn on Global core power */
	i2c_reg_write(vsc_addr, GLOBAL_CORE_CNTRL, 0);
#endif
	vsc_wp_config(vsc_addr);

	return 0;
}
#endif

int vsc3308_config(unsigned int vsc_addr, const int8_t con_arr[][2],
		unsigned int num_con)
{
	unsigned int i;
	u8 rev_id = 0;
	int ret;

	debug("VSC:Initializing VSC3308 at I2C address 0x%x"
		" for Tx\n", vsc_addr);
#ifdef CONFIG_DM_I2C
	int bus_num = 0;
	struct udevice *dev;

	ret = i2c_get_chip_for_busnum(bus_num, vsc_addr,
				      1, &dev);
	if (ret) {
		printf("%s: Cannot find udev for a bus %d\n", __func__,
		       bus_num);
		return ret;
	}

	ret = dm_i2c_read(dev, REVISION_ID_REG, &rev_id, 1);
	if (ret < 0) {
		printf("VSC:0x%x could not read REV_ID from device.\n",
		       vsc_addr);
		return ret;
	}

	if (rev_id != 0xab) {
		printf("VSC: device at address 0x%x is not VSC3316/3308.\n",
		       vsc_addr);
		return -ENODEV;
	}

	ret = vsc_if_enable(vsc_addr);
	if (ret) {
		printf("VSC:0x%x could not configured for 2-wire I/F.\n",
		       vsc_addr);
		return ret;
	}

	/* config connections - page 0x00 */
	dm_i2c_reg_write(dev, CURRENT_PAGE_REGISTER, CONNECTION_CONFIG_PAGE);

	/* Making crosspoint connections, by connecting required
	 * input to output
	 */
	for (i = 0; i < num_con ; i++)
		dm_i2c_reg_write(dev, con_arr[i][1], con_arr[i][0]);

	/*Configure Global Input ISE and gain */
	dm_i2c_reg_write(dev, GLOBAL_INPUT_ISE1, 0x12);
	dm_i2c_reg_write(dev, GLOBAL_INPUT_ISE2, 0x12);

	/* input state - page 0x13 */
	dm_i2c_reg_write(dev, CURRENT_PAGE_REGISTER, INPUT_STATE_REG);
	/* Turning ON the required input of the switch */
	for (i = 0; i < num_con ; i++)
		dm_i2c_reg_write(dev, con_arr[i][0], 0);

	/* Setting Global Input LOS threshold value */
	dm_i2c_reg_write(dev, GLOBAL_INPUT_LOS, 0x60);

	/* config output mode - page 0x23 */
	dm_i2c_reg_write(dev, CURRENT_PAGE_REGISTER, OUTPUT_MODE_PAGE);
	/* Turn ON the Output driver correspond to required output*/
	for (i = 0; i < num_con ; i++)
		dm_i2c_reg_write(dev,  con_arr[i][1], 0);

	/* configure global core control register, Turn on Global core power */
	dm_i2c_reg_write(dev, GLOBAL_CORE_CNTRL, 0);
#else
	ret = i2c_read(vsc_addr, REVISION_ID_REG, 1, &rev_id, 1);
	if (ret < 0) {
		printf("VSC:0x%x could not read REV_ID from device.\n",
			vsc_addr);
		return ret;
	}

	if (rev_id != 0xab) {
		printf("VSC: device at address 0x%x is not VSC3316/3308.\n",
			vsc_addr);
		return -ENODEV;
	}

	ret = vsc_if_enable(vsc_addr);
	if (ret) {
		printf("VSC:0x%x could not configured for 2-wire I/F.\n",
			vsc_addr);
		return ret;
	}

	/* config connections - page 0x00 */
	i2c_reg_write(vsc_addr, CURRENT_PAGE_REGISTER, CONNECTION_CONFIG_PAGE);

	/* Making crosspoint connections, by connecting required
	 * input to output */
	for (i = 0; i < num_con ; i++)
		i2c_reg_write(vsc_addr, con_arr[i][1], con_arr[i][0]);

	/*Configure Global Input ISE and gain */
	i2c_reg_write(vsc_addr, GLOBAL_INPUT_ISE1, 0x12);
	i2c_reg_write(vsc_addr, GLOBAL_INPUT_ISE2, 0x12);

	/* input state - page 0x13 */
	i2c_reg_write(vsc_addr, CURRENT_PAGE_REGISTER, INPUT_STATE_REG);
	/* Turning ON the required input of the switch */
	for (i = 0; i < num_con ; i++)
		i2c_reg_write(vsc_addr, con_arr[i][0], 0);

	/* Setting Global Input LOS threshold value */
	i2c_reg_write(vsc_addr, GLOBAL_INPUT_LOS, 0x60);

	/* config output mode - page 0x23 */
	i2c_reg_write(vsc_addr, CURRENT_PAGE_REGISTER, OUTPUT_MODE_PAGE);
	/* Turn ON the Output driver correspond to required output*/
	for (i = 0; i < num_con ; i++)
		i2c_reg_write(vsc_addr,  con_arr[i][1], 0);

	/* configure global core control register, Turn on Global core power */
	i2c_reg_write(vsc_addr, GLOBAL_CORE_CNTRL, 0);
#endif
	vsc_wp_config(vsc_addr);

	return 0;
}

void vsc_wp_config(unsigned int vsc_addr)
{
	debug("VSC:Configuring VSC at address:0x%x for WP\n", vsc_addr);

	/* For new crosspoint configuration to occur, WP bit of
	 * CORE_CONFIG_REG should be set 1 and then reset to 0 */
#ifdef CONFIG_DM_I2C
	int ret, bus_num = 0;
	struct udevice *dev;

	ret = i2c_get_chip_for_busnum(bus_num, vsc_addr,
				      1, &dev);
	if (ret) {
		printf("%s: Cannot find udev for a bus %d\n", __func__,
		       bus_num);
		return;
	}

	dm_i2c_reg_write(dev, CORE_CONFIG_REG, 0x01);
	dm_i2c_reg_write(dev, CORE_CONFIG_REG, 0x0);
#else
	i2c_reg_write(vsc_addr, CORE_CONFIG_REG, 0x01);
	i2c_reg_write(vsc_addr, CORE_CONFIG_REG, 0x0);
#endif
}