Andre Schwarz | 2a29329 | 2008-07-09 18:30:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Rich Ireland, Enterasys Networks, rireland@enterasys.com. |
| 4 | * Keith Outwater, keith_outwater@mvis.com. |
| 5 | * |
| 6 | * (C) Copyright 2008 |
| 7 | * Andre Schwarz, Matrix Vision GmbH, andre.schwarz@matrix-vision.de |
| 8 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | #include <common.h> |
| 30 | #include <ACEX1K.h> |
| 31 | #include <command.h> |
| 32 | #include "fpga.h" |
| 33 | #include "mvbc_p.h" |
| 34 | |
| 35 | #ifdef FPGA_DEBUG |
| 36 | #define fpga_debug(fmt, args...) printf("%s: "fmt, __func__, ##args) |
| 37 | #else |
| 38 | #define fpga_debug(fmt, args...) |
| 39 | #endif |
| 40 | |
| 41 | Altera_CYC2_Passive_Serial_fns altera_fns = { |
| 42 | fpga_null_fn, |
| 43 | fpga_config_fn, |
| 44 | fpga_status_fn, |
| 45 | fpga_done_fn, |
| 46 | fpga_wr_fn, |
| 47 | fpga_null_fn, |
| 48 | fpga_null_fn, |
| 49 | 0 |
| 50 | }; |
| 51 | |
| 52 | Altera_desc cyclone2 = { |
| 53 | Altera_CYC2, |
| 54 | passive_serial, |
| 55 | Altera_EP2C8_SIZE, |
| 56 | (void *) &altera_fns, |
| 57 | NULL, |
| 58 | 0 |
| 59 | }; |
| 60 | |
| 61 | DECLARE_GLOBAL_DATA_PTR; |
| 62 | |
| 63 | int mvbc_p_init_fpga(void) |
| 64 | { |
| 65 | fpga_debug("Initialize FPGA interface (reloc 0x%.8lx)\n", |
| 66 | gd->reloc_off); |
| 67 | fpga_init(gd->reloc_off); |
| 68 | fpga_add(fpga_altera, &cyclone2); |
| 69 | fpga_config_fn(0, 1, 0); |
| 70 | udelay(60); |
| 71 | |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | int fpga_null_fn(int cookie) |
| 76 | { |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int fpga_config_fn(int assert, int flush, int cookie) |
| 81 | { |
| 82 | struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio*)MPC5XXX_GPIO; |
| 83 | u32 dvo = gpio->simple_dvo; |
| 84 | |
| 85 | fpga_debug("SET config : %s\n", assert ? "low" : "high"); |
| 86 | if (assert) |
| 87 | dvo |= FPGA_CONFIG; |
| 88 | else |
| 89 | dvo &= ~FPGA_CONFIG; |
| 90 | |
| 91 | if (flush) |
| 92 | gpio->simple_dvo = dvo; |
| 93 | |
| 94 | return assert; |
| 95 | } |
| 96 | |
| 97 | int fpga_done_fn(int cookie) |
| 98 | { |
| 99 | struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio*)MPC5XXX_GPIO; |
| 100 | int result = 0; |
| 101 | |
| 102 | udelay(10); |
| 103 | fpga_debug("CONF_DONE check ... "); |
| 104 | if (gpio->simple_ival & FPGA_CONF_DONE) { |
| 105 | fpga_debug("high\n"); |
| 106 | result = 1; |
| 107 | } else |
| 108 | fpga_debug("low\n"); |
| 109 | |
| 110 | return result; |
| 111 | } |
| 112 | |
| 113 | int fpga_status_fn(int cookie) |
| 114 | { |
| 115 | struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio*)MPC5XXX_GPIO; |
| 116 | int result = 0; |
| 117 | |
| 118 | fpga_debug("STATUS check ... "); |
| 119 | if (gpio->sint_ival & FPGA_STATUS) { |
| 120 | fpga_debug("high\n"); |
| 121 | result = 1; |
| 122 | } else |
| 123 | fpga_debug("low\n"); |
| 124 | |
| 125 | return result; |
| 126 | } |
| 127 | |
| 128 | int fpga_clk_fn(int assert_clk, int flush, int cookie) |
| 129 | { |
| 130 | struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio*)MPC5XXX_GPIO; |
| 131 | u32 dvo = gpio->simple_dvo; |
| 132 | |
| 133 | fpga_debug("CLOCK %s\n", assert_clk ? "high" : "low"); |
| 134 | if (assert_clk) |
| 135 | dvo |= FPGA_CCLK; |
| 136 | else |
| 137 | dvo &= ~FPGA_CCLK; |
| 138 | |
| 139 | if (flush) |
| 140 | gpio->simple_dvo = dvo; |
| 141 | |
| 142 | return assert_clk; |
| 143 | } |
| 144 | |
| 145 | static inline int _write_fpga(u8 val) |
| 146 | { |
| 147 | int i; |
| 148 | struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio*)MPC5XXX_GPIO; |
| 149 | u32 dvo = gpio->simple_dvo; |
| 150 | |
| 151 | for (i=0; i<8; i++) { |
| 152 | dvo &= ~FPGA_CCLK; |
| 153 | gpio->simple_dvo = dvo; |
| 154 | dvo &= ~FPGA_DIN; |
| 155 | if (val & 1) |
| 156 | dvo |= FPGA_DIN; |
| 157 | gpio->simple_dvo = dvo; |
| 158 | dvo |= FPGA_CCLK; |
| 159 | gpio->simple_dvo = dvo; |
| 160 | val >>= 1; |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | int fpga_wr_fn(void *buf, size_t len, int flush, int cookie) |
| 167 | { |
| 168 | unsigned char *data = (unsigned char *) buf; |
| 169 | int i; |
| 170 | |
| 171 | fpga_debug("fpga_wr: buf %p / size %d\n", buf, len); |
| 172 | for (i = 0; i < len; i++) |
| 173 | _write_fpga(data[i]); |
| 174 | fpga_debug("\n"); |
| 175 | |
| 176 | return FPGA_SUCCESS; |
| 177 | } |