14e3e25c18d0239ccc4e1cee83150f24015b7a2e
[oweals/u-boot.git] / board / gdsys / p1022 / controlcenterd.c
1 /*
2  * (C) Copyright 2013
3  * Dirk Eibach,  Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <command.h>
26 #include <env.h>
27 #include <init.h>
28 #include <net.h>
29 #include <pci.h>
30 #include <asm/processor.h>
31 #include <asm/mmu.h>
32 #include <asm/cache.h>
33 #include <asm/immap_85xx.h>
34 #include <asm/fsl_pci.h>
35 #include <fsl_ddr_sdram.h>
36 #include <asm/fsl_serdes.h>
37 #include <asm/io.h>
38 #include <linux/libfdt.h>
39 #include <fdt_support.h>
40 #include <fsl_mdio.h>
41 #include <tsec.h>
42 #include <asm/fsl_law.h>
43 #include <netdev.h>
44 #include <i2c.h>
45 #include <pca9698.h>
46 #include <watchdog.h>
47 #include "../common/dp501.h"
48 #include "controlcenterd-id.h"
49
50 enum {
51         HWVER_100 = 0,
52         HWVER_110 = 1,
53         HWVER_120 = 2,
54 };
55
56 struct ihs_fpga {
57         u32 reflection_low;     /* 0x0000 */
58         u32 versions;           /* 0x0004 */
59         u32 fpga_version;       /* 0x0008 */
60         u32 fpga_features;      /* 0x000c */
61         u32 reserved[4];        /* 0x0010 */
62         u32 control;            /* 0x0020 */
63 };
64
65 #ifndef CONFIG_TRAILBLAZER
66 static struct pci_device_id hydra_supported[] = {
67         { 0x6d5e, 0xcdc0 },
68         {}
69 };
70
71 static void hydra_initialize(void);
72 #endif
73
74 int board_early_init_f(void)
75 {
76         ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
77         ccsr_gpio_t *pgpio = (void *)(CONFIG_SYS_MPC85xx_GPIO3_ADDR);
78
79         /* Reset eLBC_DIU and SPI_eLBC in case we are booting from SD */
80         clrsetbits_be32(&gur->pmuxcr, 0x00600000, 0x80000000);
81
82         /* Set pmuxcr to allow both i2c1 and i2c2 */
83         setbits_be32(&gur->pmuxcr, 0x00001000);
84
85         /* Set pmuxcr to enable GPIO 3_11-3_13 */
86         setbits_be32(&gur->pmuxcr, 0x00000010);
87
88         /* Set pmuxcr to enable GPIO 2_31,3_9+10 */
89         setbits_be32(&gur->pmuxcr, 0x00000020);
90
91         /* Set pmuxcr to enable GPIO 2_28-2_30 */
92         setbits_be32(&gur->pmuxcr, 0x000000c0);
93
94         /* Set pmuxcr to enable GPIO 3_20-3_22 */
95         setbits_be32(&gur->pmuxcr2, 0x03000000);
96
97         /* Set pmuxcr to enable IRQ0-2 */
98         clrbits_be32(&gur->pmuxcr, 0x00000300);
99
100         /* Set pmuxcr to disable IRQ3-11 */
101         setbits_be32(&gur->pmuxcr, 0x000000F0);
102
103         /* Read back the register to synchronize the write. */
104         in_be32(&gur->pmuxcr);
105
106         /* Set the pin muxing to enable ETSEC2. */
107         clrbits_be32(&gur->pmuxcr2, 0x001F8000);
108
109 #ifdef CONFIG_TRAILBLAZER
110         /*
111          * GPIO3_10 SPERRTRIGGER
112          */
113         setbits_be32(&pgpio->gpdir, 0x00200000);
114         clrbits_be32(&pgpio->gpdat, 0x00200000);
115         udelay(100);
116         setbits_be32(&pgpio->gpdat, 0x00200000);
117         udelay(100);
118         clrbits_be32(&pgpio->gpdat, 0x00200000);
119 #endif
120
121         /*
122          * GPIO3_11 CPU-TO-FPGA-RESET#
123          */
124         setbits_be32(&pgpio->gpdir, 0x00100000);
125         clrbits_be32(&pgpio->gpdat, 0x00100000);
126
127         /*
128          * GPIO3_21 CPU-STATUS-WATCHDOG-TRIGGER#
129          */
130         setbits_be32(&pgpio->gpdir, 0x00000400);
131
132         return 0;
133 }
134
135 int checkboard(void)
136 {
137         printf("Board: ControlCenter DIGITAL\n");
138
139         return 0;
140 }
141
142 int misc_init_r(void)
143 {
144         return 0;
145 }
146
147 /*
148  * A list of PCI and SATA slots
149  */
150 enum slot_id {
151         SLOT_PCIE1 = 1,
152         SLOT_PCIE2,
153         SLOT_PCIE3,
154         SLOT_PCIE4,
155         SLOT_PCIE5,
156         SLOT_SATA1,
157         SLOT_SATA2
158 };
159
160 /*
161  * This array maps the slot identifiers to their names on the P1022DS board.
162  */
163 static const char * const slot_names[] = {
164         [SLOT_PCIE1] = "Slot 1",
165         [SLOT_PCIE2] = "Slot 2",
166         [SLOT_PCIE3] = "Slot 3",
167         [SLOT_PCIE4] = "Slot 4",
168         [SLOT_PCIE5] = "Mini-PCIe",
169         [SLOT_SATA1] = "SATA 1",
170         [SLOT_SATA2] = "SATA 2",
171 };
172
173 /*
174  * This array maps a given SERDES configuration and SERDES device to the PCI or
175  * SATA slot that it connects to.  This mapping is hard-coded in the FPGA.
176  */
177 static u8 serdes_dev_slot[][SATA2 + 1] = {
178         [0x01] = { [PCIE3] = SLOT_PCIE4, [PCIE2] = SLOT_PCIE5 },
179         [0x02] = { [SATA1] = SLOT_SATA1, [SATA2] = SLOT_SATA2 },
180         [0x09] = { [PCIE1] = SLOT_PCIE1, [PCIE3] = SLOT_PCIE4,
181                    [PCIE2] = SLOT_PCIE5 },
182         [0x16] = { [PCIE1] = SLOT_PCIE1, [PCIE3] = SLOT_PCIE2,
183                    [PCIE2] = SLOT_PCIE3,
184                    [SATA1] = SLOT_SATA1, [SATA2] = SLOT_SATA2 },
185         [0x17] = { [PCIE1] = SLOT_PCIE1, [PCIE3] = SLOT_PCIE2,
186                    [PCIE2] = SLOT_PCIE3 },
187         [0x1a] = { [PCIE1] = SLOT_PCIE1, [PCIE2] = SLOT_PCIE3,
188                    [PCIE2] = SLOT_PCIE3,
189                    [SATA1] = SLOT_SATA1, [SATA2] = SLOT_SATA2 },
190         [0x1c] = { [PCIE1] = SLOT_PCIE1,
191                    [SATA1] = SLOT_SATA1, [SATA2] = SLOT_SATA2 },
192         [0x1e] = { [PCIE1] = SLOT_PCIE1, [PCIE3] = SLOT_PCIE3 },
193         [0x1f] = { [PCIE1] = SLOT_PCIE1 },
194 };
195
196
197 /*
198  * Returns the name of the slot to which the PCIe or SATA controller is
199  * connected
200  */
201 const char *board_serdes_name(enum srds_prtcl device)
202 {
203         ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
204         u32 pordevsr = in_be32(&gur->pordevsr);
205         unsigned int srds_cfg = (pordevsr & MPC85xx_PORDEVSR_IO_SEL) >>
206                                 MPC85xx_PORDEVSR_IO_SEL_SHIFT;
207         enum slot_id slot = serdes_dev_slot[srds_cfg][device];
208         const char *name = slot_names[slot];
209
210         if (name)
211                 return name;
212         else
213                 return "Nothing";
214 }
215
216 void hw_watchdog_reset(void)
217 {
218         ccsr_gpio_t *pgpio = (void *)(CONFIG_SYS_MPC85xx_GPIO3_ADDR);
219
220         clrbits_be32(&pgpio->gpdat, 0x00000400);
221         setbits_be32(&pgpio->gpdat, 0x00000400);
222 }
223
224 #ifdef CONFIG_TRAILBLAZER
225 int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
226 {
227         return run_command(env_get("bootcmd"), flag);
228 }
229
230 int board_early_init_r(void)
231 {
232         ccsr_gpio_t *pgpio = (void *)(CONFIG_SYS_MPC85xx_GPIO3_ADDR);
233
234         /*
235          * GPIO3_12 PPC_SYSTEMREADY#
236          */
237         setbits_be32(&pgpio->gpdir, 0x00080000);
238         setbits_be32(&pgpio->gpodr, 0x00080000);
239         clrbits_be32(&pgpio->gpdat, 0x00080000);
240
241         return ccdm_compute_self_hash();
242 }
243
244 int last_stage_init(void)
245 {
246         startup_ccdm_id_module();
247         return 0;
248 }
249
250 #else
251 void pci_init_board(void)
252 {
253         fsl_pcie_init_board(0);
254
255         hydra_initialize();
256 }
257
258 int board_early_init_r(void)
259 {
260         unsigned int k = 0;
261         ccsr_gpio_t *pgpio = (void *)(CONFIG_SYS_MPC85xx_GPIO3_ADDR);
262
263         /* wait for FPGA configuration to finish */
264         while (!pca9698_get_value(0x22, 11) && (k++ < 30))
265                 udelay(100000);
266
267         if (k > 30) {
268                 puts("FPGA configuration timed out.\n");
269         } else {
270                 /* clear FPGA reset */
271                 udelay(1000);
272                 setbits_be32(&pgpio->gpdat, 0x00100000);
273         }
274
275         /* give time for PCIe link training */
276         udelay(100000);
277
278         /*
279          * GPIO3_12 PPC_SYSTEMREADY#
280          */
281         setbits_be32(&pgpio->gpdir, 0x00080000);
282         setbits_be32(&pgpio->gpodr, 0x00080000);
283         clrbits_be32(&pgpio->gpdat, 0x00080000);
284
285         return 0;
286 }
287
288 int last_stage_init(void)
289 {
290         /* Turn on Parade DP501 */
291         pca9698_direction_output(0x22, 7, 1);
292         udelay(500000);
293
294         dp501_powerup(0x08);
295
296         startup_ccdm_id_module();
297
298         return 0;
299 }
300
301 /*
302  * Initialize on-board and/or PCI Ethernet devices
303  *
304  * Returns:
305  *      <0, error
306  *       0, no ethernet devices found
307  *      >0, number of ethernet devices initialized
308  */
309 int board_eth_init(bd_t *bis)
310 {
311         struct fsl_pq_mdio_info mdio_info;
312         struct tsec_info_struct tsec_info[2];
313         unsigned int num = 0;
314
315 #ifdef CONFIG_TSEC1
316         SET_STD_TSEC_INFO(tsec_info[num], 1);
317         num++;
318 #endif
319 #ifdef CONFIG_TSEC2
320         SET_STD_TSEC_INFO(tsec_info[num], 2);
321         num++;
322 #endif
323
324         mdio_info.regs = (struct tsec_mii_mng *)CONFIG_SYS_MDIO_BASE_ADDR;
325         mdio_info.name = DEFAULT_MII_NAME;
326         fsl_pq_mdio_init(bis, &mdio_info);
327
328         return tsec_eth_init(bis, tsec_info, num) + pci_eth_init(bis);
329 }
330
331 #ifdef CONFIG_OF_BOARD_SETUP
332 int ft_board_setup(void *blob, bd_t *bd)
333 {
334         phys_addr_t base;
335         phys_size_t size;
336
337         ft_cpu_setup(blob, bd);
338
339         base = env_get_bootm_low();
340         size = env_get_bootm_size();
341
342         fdt_fixup_memory(blob, (u64)base, (u64)size);
343
344 #ifdef CONFIG_HAS_FSL_DR_USB
345         fsl_fdt_fixup_dr_usb(blob, bd);
346 #endif
347
348         FT_FSL_PCI_SETUP;
349
350         return 0;
351 }
352 #endif
353
354 static void hydra_initialize(void)
355 {
356         unsigned int i;
357         pci_dev_t devno;
358
359         /* Find and probe all the matching PCI devices */
360         for (i = 0; (devno = pci_find_devices(hydra_supported, i)) >= 0; i++) {
361                 u32 val;
362                 struct ihs_fpga *fpga;
363                 u32 versions;
364                 u32 fpga_version;
365                 u32 fpga_features;
366
367                 unsigned hardware_version;
368                 unsigned feature_uart_channels;
369                 unsigned feature_sb_channels;
370
371                 /* Try to enable I/O accesses and bus-mastering */
372                 val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
373                 pci_write_config_dword(devno, PCI_COMMAND, val);
374
375                 /* Make sure it worked */
376                 pci_read_config_dword(devno, PCI_COMMAND, &val);
377                 if (!(val & PCI_COMMAND_MEMORY)) {
378                         puts("Can't enable I/O memory\n");
379                         continue;
380                 }
381                 if (!(val & PCI_COMMAND_MASTER)) {
382                         puts("Can't enable bus-mastering\n");
383                         continue;
384                 }
385
386                 /* read FPGA details */
387                 fpga = pci_map_bar(devno, PCI_BASE_ADDRESS_0,
388                         PCI_REGION_MEM);
389
390                 /* disable sideband clocks */
391                 writel(1, &fpga->control);
392
393                 versions = readl(&fpga->versions);
394                 fpga_version = readl(&fpga->fpga_version);
395                 fpga_features = readl(&fpga->fpga_features);
396
397                 hardware_version = versions & 0xf;
398                 feature_uart_channels = (fpga_features >> 6) & 0x1f;
399                 feature_sb_channels = fpga_features & 0x1f;
400
401                 printf("FPGA%d: ", i);
402
403                 switch (hardware_version) {
404                 case HWVER_100:
405                         printf("HW-Ver 1.00\n");
406                         break;
407
408                 case HWVER_110:
409                         printf("HW-Ver 1.10\n");
410                         break;
411
412                 case HWVER_120:
413                         printf("HW-Ver 1.20\n");
414                         break;
415
416                 default:
417                         printf("HW-Ver %d(not supported)\n",
418                                hardware_version);
419                         break;
420                 }
421
422                 printf("       FPGA V %d.%02d, features:",
423                        fpga_version / 100, fpga_version % 100);
424
425                 printf(" %d uart channel(s)", feature_uart_channels);
426                 printf(" %d sideband channel(s)\n", feature_sb_channels);
427         }
428 }
429 #endif