blob: 079dfb2e73f7ff8dcfd0d98cbc95f63556577c2e [file] [log] [blame]
Dirk Eibacha605ea72010-10-21 10:50:05 +02001/*
2 * (C) Copyright 2010
3 * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Dirk Eibacha605ea72010-10-21 10:50:05 +02006 */
7
8#include <common.h>
9#include <command.h>
Dirk Eibache50e8962013-07-25 19:28:13 +020010#include <errno.h>
Dirk Eibacha605ea72010-10-21 10:50:05 +020011#include <asm/processor.h>
12#include <asm/io.h>
13#include <asm/ppc4xx-gpio.h>
14
Dirk Eibach6e9e6c32012-04-26 03:54:22 +000015#include "405ep.h"
Dirk Eibach2da0fc02011-01-21 09:31:21 +010016#include <gdsys_fpga.h>
Dirk Eibacha605ea72010-10-21 10:50:05 +020017
Dirk Eibachedfe9fe2014-07-03 09:28:17 +020018#include "../common/dp501.h"
Dirk Eibach2da0fc02011-01-21 09:31:21 +010019#include "../common/osd.h"
Dirk Eibache50e8962013-07-25 19:28:13 +020020#include "../common/mclink.h"
21
22#include <i2c.h>
23#include <pca953x.h>
24#include <pca9698.h>
25
26#include <miiphy.h>
27
28DECLARE_GLOBAL_DATA_PTR;
Dirk Eibacha605ea72010-10-21 10:50:05 +020029
Dirk Eibach6e9e6c32012-04-26 03:54:22 +000030#define LATCH0_BASE (CONFIG_SYS_LATCH_BASE)
31#define LATCH1_BASE (CONFIG_SYS_LATCH_BASE + 0x100)
32#define LATCH2_BASE (CONFIG_SYS_LATCH_BASE + 0x200)
33
Dirk Eibachd78951d2013-08-09 10:52:52 +020034#define MAX_MUX_CHANNELS 2
35
Dirk Eibacha605ea72010-10-21 10:50:05 +020036enum {
37 UNITTYPE_MAIN_SERVER = 0,
38 UNITTYPE_MAIN_USER = 1,
39 UNITTYPE_VIDEO_SERVER = 2,
40 UNITTYPE_VIDEO_USER = 3,
41};
42
43enum {
44 HWVER_100 = 0,
45 HWVER_104 = 1,
46 HWVER_110 = 2,
Dirk Eibache50e8962013-07-25 19:28:13 +020047 HWVER_120 = 3,
48 HWVER_200 = 4,
49 HWVER_210 = 5,
Dirk Eibacha8089702013-08-09 10:52:51 +020050 HWVER_220 = 6,
51 HWVER_230 = 7,
Dirk Eibache50e8962013-07-25 19:28:13 +020052};
53
54enum {
55 FPGA_HWVER_200 = 0,
56 FPGA_HWVER_210 = 1,
Dirk Eibacha605ea72010-10-21 10:50:05 +020057};
58
59enum {
60 COMPRESSION_NONE = 0,
Dirk Eibache50e8962013-07-25 19:28:13 +020061 COMPRESSION_TYPE1_DELTA = 1,
62 COMPRESSION_TYPE1_TYPE2_DELTA = 3,
Dirk Eibacha605ea72010-10-21 10:50:05 +020063};
64
65enum {
66 AUDIO_NONE = 0,
67 AUDIO_TX = 1,
68 AUDIO_RX = 2,
69 AUDIO_RXTX = 3,
70};
71
72enum {
73 SYSCLK_147456 = 0,
74};
75
76enum {
77 RAM_DDR2_32 = 0,
Dirk Eibache50e8962013-07-25 19:28:13 +020078 RAM_DDR3_32 = 1,
Dirk Eibacha605ea72010-10-21 10:50:05 +020079};
80
Dirk Eibache50e8962013-07-25 19:28:13 +020081enum {
Dirk Eibacha8089702013-08-09 10:52:51 +020082 CARRIER_SPEED_1G = 0,
83 CARRIER_SPEED_2_5G = 1,
84};
85
86enum {
Dirk Eibache50e8962013-07-25 19:28:13 +020087 MCFPGA_DONE = 1 << 0,
88 MCFPGA_INIT_N = 1 << 1,
89 MCFPGA_PROGRAM_N = 1 << 2,
90 MCFPGA_UPDATE_ENABLE_N = 1 << 3,
91 MCFPGA_RESET_N = 1 << 4,
92};
93
94enum {
95 GPIO_MDC = 1 << 14,
96 GPIO_MDIO = 1 << 15,
97};
98
99unsigned int mclink_fpgacount;
Dirk Eibachaba27ac2013-06-26 16:04:26 +0200100struct ihs_fpga *fpga_ptr[] = CONFIG_SYS_FPGA_PTR;
101
Dirk Eibachedfe9fe2014-07-03 09:28:17 +0200102int dp501_i2c[] = CONFIG_SYS_DP501_I2C;
103
Dirk Eibache50e8962013-07-25 19:28:13 +0200104static int setup_88e1518(const char *bus, unsigned char addr);
Dirk Eibache50e8962013-07-25 19:28:13 +0200105
106int fpga_set_reg(u32 fpga, u16 *reg, off_t regoff, u16 data)
107{
108 int res;
109
110 switch (fpga) {
111 case 0:
112 out_le16(reg, data);
113 break;
114 default:
115 res = mclink_send(fpga - 1, regoff, data);
116 if (res < 0) {
117 printf("mclink_send reg %02lx data %04x returned %d\n",
118 regoff, data, res);
119 return res;
120 }
121 break;
122 }
123
124 return 0;
125}
126
127int fpga_get_reg(u32 fpga, u16 *reg, off_t regoff, u16 *data)
128{
129 int res;
130
131 switch (fpga) {
132 case 0:
133 *data = in_le16(reg);
134 break;
135 default:
136 if (fpga > mclink_fpgacount)
137 return -EINVAL;
138 res = mclink_receive(fpga - 1, regoff, data);
139 if (res < 0) {
140 printf("mclink_receive reg %02lx returned %d\n",
141 regoff, res);
142 return res;
143 }
144 }
145
146 return 0;
147}
148
Dirk Eibacha605ea72010-10-21 10:50:05 +0200149/*
150 * Check Board Identity:
151 */
152int checkboard(void)
153{
Dirk Eibachb19bf832012-04-26 03:54:23 +0000154 char *s = getenv("serial#");
155
156 puts("Board: ");
157
158 puts("IoCon");
159
160 if (s != NULL) {
161 puts(", serial# ");
162 puts(s);
163 }
164
165 puts("\n");
166
167 return 0;
168}
169
Dirk Eibachd78951d2013-08-09 10:52:52 +0200170static void print_fpga_info(unsigned int fpga, bool rgmii2_present)
Dirk Eibachb19bf832012-04-26 03:54:23 +0000171{
Dirk Eibachaba27ac2013-06-26 16:04:26 +0200172 u16 versions;
173 u16 fpga_version;
174 u16 fpga_features;
Dirk Eibacha605ea72010-10-21 10:50:05 +0200175 unsigned unit_type;
176 unsigned hardware_version;
177 unsigned feature_compression;
178 unsigned feature_osd;
179 unsigned feature_audio;
180 unsigned feature_sysclock;
181 unsigned feature_ramconfig;
Dirk Eibacha8089702013-08-09 10:52:51 +0200182 unsigned feature_carrier_speed;
Dirk Eibacha605ea72010-10-21 10:50:05 +0200183 unsigned feature_carriers;
184 unsigned feature_video_channels;
Dirk Eibacha8089702013-08-09 10:52:51 +0200185
Dirk Eibache50e8962013-07-25 19:28:13 +0200186 int legacy = get_fpga_state(0) & FPGA_STATE_PLATFORM;
Dirk Eibacha605ea72010-10-21 10:50:05 +0200187
Dirk Eibachaba27ac2013-06-26 16:04:26 +0200188 FPGA_GET_REG(0, versions, &versions);
189 FPGA_GET_REG(0, fpga_version, &fpga_version);
190 FPGA_GET_REG(0, fpga_features, &fpga_features);
191
Dirk Eibacha605ea72010-10-21 10:50:05 +0200192 unit_type = (versions & 0xf000) >> 12;
Dirk Eibacha605ea72010-10-21 10:50:05 +0200193 feature_compression = (fpga_features & 0xe000) >> 13;
194 feature_osd = fpga_features & (1<<11);
195 feature_audio = (fpga_features & 0x0600) >> 9;
196 feature_sysclock = (fpga_features & 0x0180) >> 7;
197 feature_ramconfig = (fpga_features & 0x0060) >> 5;
Dirk Eibacha8089702013-08-09 10:52:51 +0200198 feature_carrier_speed = fpga_features & (1<<4);
Dirk Eibacha605ea72010-10-21 10:50:05 +0200199 feature_carriers = (fpga_features & 0x000c) >> 2;
200 feature_video_channels = fpga_features & 0x0003;
201
Dirk Eibache50e8962013-07-25 19:28:13 +0200202 if (legacy)
203 printf("legacy ");
204
Dirk Eibacha605ea72010-10-21 10:50:05 +0200205 switch (unit_type) {
206 case UNITTYPE_MAIN_USER:
207 printf("Mainchannel");
208 break;
209
210 case UNITTYPE_VIDEO_USER:
211 printf("Videochannel");
212 break;
213
214 default:
215 printf("UnitType %d(not supported)", unit_type);
216 break;
217 }
218
Dirk Eibache50e8962013-07-25 19:28:13 +0200219 if (unit_type == UNITTYPE_MAIN_USER) {
220 if (legacy)
221 hardware_version =
222 (in_le16((void *)LATCH2_BASE)>>8) & 0x0f;
223 else
224 hardware_version =
225 (!!pca9698_get_value(0x20, 24) << 0)
226 | (!!pca9698_get_value(0x20, 25) << 1)
227 | (!!pca9698_get_value(0x20, 26) << 2)
228 | (!!pca9698_get_value(0x20, 27) << 3);
229 switch (hardware_version) {
230 case HWVER_100:
231 printf(" HW-Ver 1.00,");
232 break;
Dirk Eibacha605ea72010-10-21 10:50:05 +0200233
Dirk Eibache50e8962013-07-25 19:28:13 +0200234 case HWVER_104:
235 printf(" HW-Ver 1.04,");
236 break;
Dirk Eibacha605ea72010-10-21 10:50:05 +0200237
Dirk Eibache50e8962013-07-25 19:28:13 +0200238 case HWVER_110:
239 printf(" HW-Ver 1.10,");
240 break;
Dirk Eibacha605ea72010-10-21 10:50:05 +0200241
Dirk Eibache50e8962013-07-25 19:28:13 +0200242 case HWVER_120:
243 printf(" HW-Ver 1.20-1.21,");
244 break;
245
246 case HWVER_200:
247 printf(" HW-Ver 2.00,");
248 break;
249
250 case HWVER_210:
251 printf(" HW-Ver 2.10,");
252 break;
253
Dirk Eibacha8089702013-08-09 10:52:51 +0200254 case HWVER_220:
255 printf(" HW-Ver 2.20,");
256 break;
257
258 case HWVER_230:
259 printf(" HW-Ver 2.30,");
260 break;
261
Dirk Eibache50e8962013-07-25 19:28:13 +0200262 default:
263 printf(" HW-Ver %d(not supported),",
264 hardware_version);
265 break;
266 }
Dirk Eibachd78951d2013-08-09 10:52:52 +0200267 if (rgmii2_present)
268 printf(" RGMII2,");
Dirk Eibacha605ea72010-10-21 10:50:05 +0200269 }
270
Dirk Eibache50e8962013-07-25 19:28:13 +0200271 if (unit_type == UNITTYPE_VIDEO_USER) {
272 hardware_version = versions & 0x000f;
273 switch (hardware_version) {
274 case FPGA_HWVER_200:
275 printf(" HW-Ver 2.00,");
276 break;
277
278 case FPGA_HWVER_210:
279 printf(" HW-Ver 2.10,");
280 break;
281
282 default:
283 printf(" HW-Ver %d(not supported),",
284 hardware_version);
285 break;
286 }
287 }
288
289 printf(" FPGA V %d.%02d\n features:",
290 fpga_version / 100, fpga_version % 100);
Dirk Eibacha605ea72010-10-21 10:50:05 +0200291
292
293 switch (feature_compression) {
294 case COMPRESSION_NONE:
295 printf(" no compression");
296 break;
297
298 case COMPRESSION_TYPE1_DELTA:
299 printf(" type1-deltacompression");
300 break;
301
Dirk Eibache50e8962013-07-25 19:28:13 +0200302 case COMPRESSION_TYPE1_TYPE2_DELTA:
303 printf(" type1-deltacompression, type2-inlinecompression");
304 break;
305
Dirk Eibacha605ea72010-10-21 10:50:05 +0200306 default:
307 printf(" compression %d(not supported)", feature_compression);
308 break;
309 }
310
311 printf(", %sosd", feature_osd ? "" : "no ");
312
313 switch (feature_audio) {
314 case AUDIO_NONE:
315 printf(", no audio");
316 break;
317
318 case AUDIO_TX:
319 printf(", audio tx");
320 break;
321
322 case AUDIO_RX:
323 printf(", audio rx");
324 break;
325
326 case AUDIO_RXTX:
327 printf(", audio rx+tx");
328 break;
329
330 default:
331 printf(", audio %d(not supported)", feature_audio);
332 break;
333 }
334
335 puts(",\n ");
336
337 switch (feature_sysclock) {
338 case SYSCLK_147456:
339 printf("clock 147.456 MHz");
340 break;
341
342 default:
343 printf("clock %d(not supported)", feature_sysclock);
344 break;
345 }
346
347 switch (feature_ramconfig) {
348 case RAM_DDR2_32:
349 printf(", RAM 32 bit DDR2");
350 break;
351
Dirk Eibache50e8962013-07-25 19:28:13 +0200352 case RAM_DDR3_32:
353 printf(", RAM 32 bit DDR3");
354 break;
355
Dirk Eibacha605ea72010-10-21 10:50:05 +0200356 default:
357 printf(", RAM %d(not supported)", feature_ramconfig);
358 break;
359 }
360
Dirk Eibacha8089702013-08-09 10:52:51 +0200361 printf(", %d carrier(s) %s", feature_carriers,
362 feature_carrier_speed ? "2.5Gbit/s" : "1Gbit/s");
Dirk Eibacha605ea72010-10-21 10:50:05 +0200363
364 printf(", %d video channel(s)\n", feature_video_channels);
Dirk Eibacha605ea72010-10-21 10:50:05 +0200365}
366
367int last_stage_init(void)
368{
Dirk Eibache50e8962013-07-25 19:28:13 +0200369 int slaves;
370 unsigned int k;
Dirk Eibachd78951d2013-08-09 10:52:52 +0200371 unsigned int mux_ch;
Dirk Eibache50e8962013-07-25 19:28:13 +0200372 unsigned char mclink_controllers[] = { 0x24, 0x25, 0x26 };
373 int legacy = get_fpga_state(0) & FPGA_STATE_PLATFORM;
Dirk Eibacha8089702013-08-09 10:52:51 +0200374 u16 fpga_features;
375 int feature_carrier_speed = fpga_features & (1<<4);
Dirk Eibachd78951d2013-08-09 10:52:52 +0200376 bool ch0_rgmii2_present = false;
Dirk Eibachedfe9fe2014-07-03 09:28:17 +0200377 int old_bus = i2c_get_bus_num();
Dirk Eibacha8089702013-08-09 10:52:51 +0200378
379 FPGA_GET_REG(0, fpga_features, &fpga_features);
Dirk Eibachb19bf832012-04-26 03:54:23 +0000380
Dirk Eibachedfe9fe2014-07-03 09:28:17 +0200381 /* Turn on Parade DP501 */
382 pca9698_direction_output(0x20, 9, 1);
383 udelay(500000);
384
385 i2c_set_bus_num(dp501_i2c[0]);
386 dp501_powerup(0x08);
387 i2c_set_bus_num(old_bus);
388
Dirk Eibachd78951d2013-08-09 10:52:52 +0200389 if (!legacy)
390 ch0_rgmii2_present = !pca9698_get_value(0x20, 30);
391
392 print_fpga_info(0, ch0_rgmii2_present);
Dirk Eibache50e8962013-07-25 19:28:13 +0200393 osd_probe(0);
394
395 /* wait for FPGA done */
396 for (k = 0; k < ARRAY_SIZE(mclink_controllers); ++k) {
397 unsigned int ctr = 0;
398
399 if (i2c_probe(mclink_controllers[k]))
400 continue;
401
402 while (!(pca953x_get_val(mclink_controllers[k])
403 & MCFPGA_DONE)) {
404 udelay(100000);
405 if (ctr++ > 5) {
406 printf("no done for mclink_controller %d\n", k);
407 break;
408 }
409 }
410 }
411
Dirk Eibacha8089702013-08-09 10:52:51 +0200412 if (!legacy && (feature_carrier_speed == CARRIER_SPEED_1G)) {
Dirk Eibache50e8962013-07-25 19:28:13 +0200413 miiphy_register(bb_miiphy_buses[0].name, bb_miiphy_read,
414 bb_miiphy_write);
Dirk Eibachd78951d2013-08-09 10:52:52 +0200415 for (mux_ch = 0; mux_ch < MAX_MUX_CHANNELS; ++mux_ch) {
416 if ((mux_ch == 1) && !ch0_rgmii2_present)
417 continue;
418
Dirk Eibachf24c8e82013-08-09 10:52:53 +0200419 setup_88e1518(bb_miiphy_buses[0].name, mux_ch);
Dirk Eibache50e8962013-07-25 19:28:13 +0200420 }
421 }
422
423 /* wait for slave-PLLs to be up and running */
424 udelay(500000);
425
426 mclink_fpgacount = CONFIG_SYS_MCLINK_MAX;
427 slaves = mclink_probe();
428 mclink_fpgacount = 0;
429
430 if (slaves <= 0)
431 return 0;
432
433 mclink_fpgacount = slaves;
434
435 for (k = 1; k <= slaves; ++k) {
Dirk Eibacha8089702013-08-09 10:52:51 +0200436 FPGA_GET_REG(k, fpga_features, &fpga_features);
437 feature_carrier_speed = fpga_features & (1<<4);
438
Dirk Eibachd78951d2013-08-09 10:52:52 +0200439 print_fpga_info(k, false);
Dirk Eibache50e8962013-07-25 19:28:13 +0200440 osd_probe(k);
Dirk Eibacha8089702013-08-09 10:52:51 +0200441 if (feature_carrier_speed == CARRIER_SPEED_1G) {
442 miiphy_register(bb_miiphy_buses[k].name,
443 bb_miiphy_read, bb_miiphy_write);
Dirk Eibachf24c8e82013-08-09 10:52:53 +0200444 setup_88e1518(bb_miiphy_buses[k].name, 0);
Dirk Eibache50e8962013-07-25 19:28:13 +0200445 }
446 }
447
448 return 0;
Dirk Eibacha605ea72010-10-21 10:50:05 +0200449}
450
451/*
452 * provide access to fpga gpios (for I2C bitbang)
Dirk Eibachaba27ac2013-06-26 16:04:26 +0200453 * (these may look all too simple but make iocon.h much more readable)
Dirk Eibacha605ea72010-10-21 10:50:05 +0200454 */
Dirk Eibache50e8962013-07-25 19:28:13 +0200455void fpga_gpio_set(unsigned int bus, int pin)
Dirk Eibacha605ea72010-10-21 10:50:05 +0200456{
Dirk Eibache50e8962013-07-25 19:28:13 +0200457 FPGA_SET_REG(bus, gpio.set, pin);
Dirk Eibacha605ea72010-10-21 10:50:05 +0200458}
459
Dirk Eibache50e8962013-07-25 19:28:13 +0200460void fpga_gpio_clear(unsigned int bus, int pin)
Dirk Eibacha605ea72010-10-21 10:50:05 +0200461{
Dirk Eibache50e8962013-07-25 19:28:13 +0200462 FPGA_SET_REG(bus, gpio.clear, pin);
Dirk Eibacha605ea72010-10-21 10:50:05 +0200463}
464
Dirk Eibache50e8962013-07-25 19:28:13 +0200465int fpga_gpio_get(unsigned int bus, int pin)
Dirk Eibacha605ea72010-10-21 10:50:05 +0200466{
Dirk Eibachaba27ac2013-06-26 16:04:26 +0200467 u16 val;
468
Dirk Eibache50e8962013-07-25 19:28:13 +0200469 FPGA_GET_REG(bus, gpio.read, &val);
Dirk Eibachaba27ac2013-06-26 16:04:26 +0200470
471 return val & pin;
Dirk Eibacha605ea72010-10-21 10:50:05 +0200472}
Dirk Eibach6e9e6c32012-04-26 03:54:22 +0000473
474void gd405ep_init(void)
475{
Dirk Eibache50e8962013-07-25 19:28:13 +0200476 unsigned int k;
477
478 if (i2c_probe(0x20)) { /* i2c_probe returns 0 on success */
479 for (k = 0; k < CONFIG_SYS_FPGA_COUNT; ++k)
480 gd->arch.fpga_state[k] |= FPGA_STATE_PLATFORM;
481 } else {
482 pca9698_direction_output(0x20, 4, 1);
483 }
Dirk Eibach6e9e6c32012-04-26 03:54:22 +0000484}
485
486void gd405ep_set_fpga_reset(unsigned state)
487{
Dirk Eibache50e8962013-07-25 19:28:13 +0200488 int legacy = get_fpga_state(0) & FPGA_STATE_PLATFORM;
489
490 if (legacy) {
491 if (state) {
492 out_le16((void *)LATCH0_BASE, CONFIG_SYS_LATCH0_RESET);
493 out_le16((void *)LATCH1_BASE, CONFIG_SYS_LATCH1_RESET);
494 } else {
495 out_le16((void *)LATCH0_BASE, CONFIG_SYS_LATCH0_BOOT);
496 out_le16((void *)LATCH1_BASE, CONFIG_SYS_LATCH1_BOOT);
497 }
Dirk Eibach6e9e6c32012-04-26 03:54:22 +0000498 } else {
Dirk Eibache50e8962013-07-25 19:28:13 +0200499 pca9698_set_value(0x20, 4, state ? 0 : 1);
Dirk Eibach6e9e6c32012-04-26 03:54:22 +0000500 }
501}
502
503void gd405ep_setup_hw(void)
504{
505 /*
506 * set "startup-finished"-gpios
507 */
508 gpio_write_bit(21, 0);
509 gpio_write_bit(22, 1);
510}
511
512int gd405ep_get_fpga_done(unsigned fpga)
513{
Dirk Eibache50e8962013-07-25 19:28:13 +0200514 int legacy = get_fpga_state(0) & FPGA_STATE_PLATFORM;
515
516 if (legacy)
517 return in_le16((void *)LATCH2_BASE)
518 & CONFIG_SYS_FPGA_DONE(fpga);
519 else
520 return pca9698_get_value(0x20, 20);
521}
522
523/*
524 * FPGA MII bitbang implementation
525 */
526
527struct fpga_mii {
528 unsigned fpga;
529 int mdio;
530} fpga_mii[] = {
531 { 0, 1},
532 { 1, 1},
533 { 2, 1},
534 { 3, 1},
535};
536
537static int mii_dummy_init(struct bb_miiphy_bus *bus)
538{
539 return 0;
540}
541
542static int mii_mdio_active(struct bb_miiphy_bus *bus)
543{
544 struct fpga_mii *fpga_mii = bus->priv;
545
546 if (fpga_mii->mdio)
547 FPGA_SET_REG(fpga_mii->fpga, gpio.set, GPIO_MDIO);
548 else
549 FPGA_SET_REG(fpga_mii->fpga, gpio.clear, GPIO_MDIO);
550
551 return 0;
552}
553
554static int mii_mdio_tristate(struct bb_miiphy_bus *bus)
555{
556 struct fpga_mii *fpga_mii = bus->priv;
557
558 FPGA_SET_REG(fpga_mii->fpga, gpio.set, GPIO_MDIO);
559
560 return 0;
561}
562
563static int mii_set_mdio(struct bb_miiphy_bus *bus, int v)
564{
565 struct fpga_mii *fpga_mii = bus->priv;
566
567 if (v)
568 FPGA_SET_REG(fpga_mii->fpga, gpio.set, GPIO_MDIO);
569 else
570 FPGA_SET_REG(fpga_mii->fpga, gpio.clear, GPIO_MDIO);
571
572 fpga_mii->mdio = v;
573
574 return 0;
575}
576
577static int mii_get_mdio(struct bb_miiphy_bus *bus, int *v)
578{
579 u16 gpio;
580 struct fpga_mii *fpga_mii = bus->priv;
581
582 FPGA_GET_REG(fpga_mii->fpga, gpio.read, &gpio);
583
584 *v = ((gpio & GPIO_MDIO) != 0);
585
586 return 0;
587}
588
589static int mii_set_mdc(struct bb_miiphy_bus *bus, int v)
590{
591 struct fpga_mii *fpga_mii = bus->priv;
592
593 if (v)
594 FPGA_SET_REG(fpga_mii->fpga, gpio.set, GPIO_MDC);
595 else
596 FPGA_SET_REG(fpga_mii->fpga, gpio.clear, GPIO_MDC);
597
598 return 0;
599}
600
601static int mii_delay(struct bb_miiphy_bus *bus)
602{
603 udelay(1);
604
605 return 0;
606}
607
608struct bb_miiphy_bus bb_miiphy_buses[] = {
609 {
Dirk Eibachd78951d2013-08-09 10:52:52 +0200610 .name = "board0",
Dirk Eibache50e8962013-07-25 19:28:13 +0200611 .init = mii_dummy_init,
612 .mdio_active = mii_mdio_active,
613 .mdio_tristate = mii_mdio_tristate,
614 .set_mdio = mii_set_mdio,
615 .get_mdio = mii_get_mdio,
616 .set_mdc = mii_set_mdc,
617 .delay = mii_delay,
618 .priv = &fpga_mii[0],
619 },
620 {
Dirk Eibachd78951d2013-08-09 10:52:52 +0200621 .name = "board1",
Dirk Eibache50e8962013-07-25 19:28:13 +0200622 .init = mii_dummy_init,
623 .mdio_active = mii_mdio_active,
624 .mdio_tristate = mii_mdio_tristate,
625 .set_mdio = mii_set_mdio,
626 .get_mdio = mii_get_mdio,
627 .set_mdc = mii_set_mdc,
628 .delay = mii_delay,
629 .priv = &fpga_mii[1],
630 },
631 {
Dirk Eibachd78951d2013-08-09 10:52:52 +0200632 .name = "board2",
Dirk Eibache50e8962013-07-25 19:28:13 +0200633 .init = mii_dummy_init,
634 .mdio_active = mii_mdio_active,
635 .mdio_tristate = mii_mdio_tristate,
636 .set_mdio = mii_set_mdio,
637 .get_mdio = mii_get_mdio,
638 .set_mdc = mii_set_mdc,
639 .delay = mii_delay,
640 .priv = &fpga_mii[2],
641 },
642 {
Dirk Eibachd78951d2013-08-09 10:52:52 +0200643 .name = "board3",
Dirk Eibache50e8962013-07-25 19:28:13 +0200644 .init = mii_dummy_init,
645 .mdio_active = mii_mdio_active,
646 .mdio_tristate = mii_mdio_tristate,
647 .set_mdio = mii_set_mdio,
648 .get_mdio = mii_get_mdio,
649 .set_mdc = mii_set_mdc,
650 .delay = mii_delay,
651 .priv = &fpga_mii[3],
652 },
653};
654
655int bb_miiphy_buses_num = sizeof(bb_miiphy_buses) /
656 sizeof(bb_miiphy_buses[0]);
657
Dirk Eibachf24c8e82013-08-09 10:52:53 +0200658enum {
659 MIICMD_SET,
660 MIICMD_MODIFY,
661 MIICMD_VERIFY_VALUE,
662 MIICMD_WAIT_FOR_VALUE,
663};
Dirk Eibache50e8962013-07-25 19:28:13 +0200664
Dirk Eibachf24c8e82013-08-09 10:52:53 +0200665struct mii_setupcmd {
666 u8 token;
Dirk Eibache50e8962013-07-25 19:28:13 +0200667 u8 reg;
668 u16 data;
Dirk Eibachf24c8e82013-08-09 10:52:53 +0200669 u16 mask;
670 u32 timeout;
Dirk Eibache50e8962013-07-25 19:28:13 +0200671};
672
Dirk Eibachf24c8e82013-08-09 10:52:53 +0200673/*
674 * verify we are talking to a 88e1518
675 */
676struct mii_setupcmd verify_88e1518[] = {
677 { MIICMD_SET, 22, 0x0000 },
678 { MIICMD_VERIFY_VALUE, 2, 0x0141, 0xffff },
679 { MIICMD_VERIFY_VALUE, 3, 0x0dd0, 0xfff0 },
680};
681
682/*
683 * workaround for erratum mentioned in 88E1518 release notes
684 */
685struct mii_setupcmd fixup_88e1518[] = {
686 { MIICMD_SET, 22, 0x00ff },
687 { MIICMD_SET, 17, 0x214b },
688 { MIICMD_SET, 16, 0x2144 },
689 { MIICMD_SET, 17, 0x0c28 },
690 { MIICMD_SET, 16, 0x2146 },
691 { MIICMD_SET, 17, 0xb233 },
692 { MIICMD_SET, 16, 0x214d },
693 { MIICMD_SET, 17, 0xcc0c },
694 { MIICMD_SET, 16, 0x2159 },
695 { MIICMD_SET, 22, 0x00fb },
696 { MIICMD_SET, 7, 0xc00d },
697 { MIICMD_SET, 22, 0x0000 },
698};
699
700/*
701 * default initialization:
702 * - set RGMII receive timing to "receive clock transition when data stable"
703 * - set RGMII transmit timing to "transmit clock internally delayed"
704 * - set RGMII output impedance target to 78,8 Ohm
705 * - run output impedance calibration
706 * - set autonegotiation advertise to 1000FD only
707 */
708struct mii_setupcmd default_88e1518[] = {
709 { MIICMD_SET, 22, 0x0002 },
710 { MIICMD_MODIFY, 21, 0x0030, 0x0030 },
711 { MIICMD_MODIFY, 25, 0x0000, 0x0003 },
712 { MIICMD_MODIFY, 24, 0x8000, 0x8000 },
713 { MIICMD_WAIT_FOR_VALUE, 24, 0x4000, 0x4000, 2000 },
714 { MIICMD_SET, 22, 0x0000 },
715 { MIICMD_MODIFY, 4, 0x0000, 0x01e0 },
716 { MIICMD_MODIFY, 9, 0x0200, 0x0300 },
717};
718
719/*
720 * turn off CLK125 for PHY daughterboard
721 */
722struct mii_setupcmd ch1fix_88e1518[] = {
723 { MIICMD_SET, 22, 0x0002 },
724 { MIICMD_MODIFY, 16, 0x0006, 0x0006 },
725 { MIICMD_SET, 22, 0x0000 },
726};
727
728/*
729 * perform copper software reset
730 */
731struct mii_setupcmd swreset_88e1518[] = {
732 { MIICMD_SET, 22, 0x0000 },
733 { MIICMD_MODIFY, 0, 0x8000, 0x8000 },
734 { MIICMD_WAIT_FOR_VALUE, 0, 0x0000, 0x8000, 2000 },
735};
736
737static int process_setupcmd(const char *bus, unsigned char addr,
738 struct mii_setupcmd *setupcmd)
739{
740 int res;
741 u8 reg = setupcmd->reg;
742 u16 data = setupcmd->data;
743 u16 mask = setupcmd->mask;
744 u32 timeout = setupcmd->timeout;
745 u16 orig_data;
746 unsigned long start;
747
748 debug("mii %s:%u reg %2u ", bus, addr, reg);
749
750 switch (setupcmd->token) {
751 case MIICMD_MODIFY:
752 res = miiphy_read(bus, addr, reg, &orig_data);
753 if (res)
754 break;
755 debug("is %04x. (value %04x mask %04x) ", orig_data, data,
756 mask);
757 data = (orig_data & ~mask) | (data & mask);
758 case MIICMD_SET:
759 debug("=> %04x\n", data);
760 res = miiphy_write(bus, addr, reg, data);
761 break;
762 case MIICMD_VERIFY_VALUE:
763 res = miiphy_read(bus, addr, reg, &orig_data);
764 if (res)
765 break;
766 if ((orig_data & mask) != (data & mask))
767 res = -1;
768 debug("(value %04x mask %04x) == %04x? %s\n", data, mask,
769 orig_data, res ? "FAIL" : "PASS");
770 break;
771 case MIICMD_WAIT_FOR_VALUE:
772 res = -1;
773 start = get_timer(0);
774 while ((res != 0) && (get_timer(start) < timeout)) {
775 res = miiphy_read(bus, addr, reg, &orig_data);
776 if (res)
777 continue;
778 if ((orig_data & mask) != (data & mask))
779 res = -1;
780 }
781 debug("(value %04x mask %04x) == %04x? %s after %lu ms\n", data,
782 mask, orig_data, res ? "FAIL" : "PASS",
783 get_timer(start));
784 break;
785 default:
786 res = -1;
787 break;
788 }
789
790 return res;
791}
792
793static int process_setup(const char *bus, unsigned char addr,
794 struct mii_setupcmd *setupcmd, unsigned int count)
795{
796 int res = 0;
797 unsigned int k;
798
799 for (k = 0; k < count; ++k) {
800 res = process_setupcmd(bus, addr, &setupcmd[k]);
801 if (res) {
802 printf("mii cmd %u on bus %s addr %u failed, aborting setup",
803 setupcmd[k].token, bus, addr);
804 break;
805 }
806 }
807
808 return res;
809}
810
Dirk Eibache50e8962013-07-25 19:28:13 +0200811static int setup_88e1518(const char *bus, unsigned char addr)
812{
Dirk Eibachf24c8e82013-08-09 10:52:53 +0200813 int res;
Dirk Eibache50e8962013-07-25 19:28:13 +0200814
Dirk Eibachf24c8e82013-08-09 10:52:53 +0200815 res = process_setup(bus, addr,
816 verify_88e1518, ARRAY_SIZE(verify_88e1518));
817 if (res)
818 return res;
819
820 res = process_setup(bus, addr,
821 fixup_88e1518, ARRAY_SIZE(fixup_88e1518));
822 if (res)
823 return res;
824
825 res = process_setup(bus, addr,
826 default_88e1518, ARRAY_SIZE(default_88e1518));
827 if (res)
828 return res;
829
830 if (addr) {
831 res = process_setup(bus, addr,
832 ch1fix_88e1518, ARRAY_SIZE(ch1fix_88e1518));
833 if (res)
834 return res;
Dirk Eibache50e8962013-07-25 19:28:13 +0200835 }
836
Dirk Eibachf24c8e82013-08-09 10:52:53 +0200837 res = process_setup(bus, addr,
838 swreset_88e1518, ARRAY_SIZE(swreset_88e1518));
839 if (res)
840 return res;
841
Dirk Eibache50e8962013-07-25 19:28:13 +0200842 return 0;
Dirk Eibach6e9e6c32012-04-26 03:54:22 +0000843}