aboutsummaryrefslogtreecommitdiff
path: root/drivers/staging/meilhaus/mefirmware.c
blob: c07d202e8cb51efe49c509295453d705691fe587 (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
/**
 * @file mefirmware.c
 *
 * @brief Implements the firmware handling.
 * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
 * @author Guenter Gebhardt
 * @author Krzysztof Gantzke	(k.gantzke@meilhaus.de)
 */

/***************************************************************************
 *   Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)     *
 *   Copyright (C) 2007 by Krzysztof Gantzke k.gantzke@meilhaus.de         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef __KERNEL__
# define __KERNEL__
#endif

#ifndef KBUILD_MODNAME
#  define KBUILD_MODNAME KBUILD_STR(mefirmware)
#endif

#include <linux/pci.h>
#include <linux/delay.h>

#include <linux/firmware.h>

#include "meplx_reg.h"
#include "medebug.h"

#include "mefirmware.h"

int me_xilinx_download(unsigned long register_base_control,
		       unsigned long register_base_data,
		       struct device *dev, const char *firmware_name)
{
	int err = ME_ERRNO_FIRMWARE;
	uint32_t value = 0;
	int idx = 0;

	const struct firmware *fw;

	PDEBUG("executed.\n");

	if (!firmware_name) {
		PERROR("Request for firmware failed. No name provided. \n");
		return err;
	}

	PINFO("Request '%s' firmware.\n", firmware_name);
	err = request_firmware(&fw, firmware_name, dev);

	if (err) {
		PERROR("Request for firmware failed.\n");
		return err;
	}
	// Set PLX local interrupt 2 polarity to high.
	// Interrupt is thrown by init pin of xilinx.
	outl(PLX_INTCSR_LOCAL_INT2_POL, register_base_control + PLX_INTCSR);

	// Set /CS and /WRITE of the Xilinx
	value = inl(register_base_control + PLX_ICR);
	value |= ME_FIRMWARE_CS_WRITE;
	outl(value, register_base_control + PLX_ICR);

	// Init Xilinx with CS1
	inl(register_base_data + ME_XILINX_CS1_REG);

	// Wait for init to complete
	udelay(20);

	// Checkl /INIT pin
	if (!
	    (inl(register_base_control + PLX_INTCSR) &
	     PLX_INTCSR_LOCAL_INT2_STATE)) {
		PERROR("Can't init Xilinx.\n");
		release_firmware(fw);
		return -EIO;
	}
	// Reset /CS and /WRITE of the Xilinx
	value = inl(register_base_control + PLX_ICR);
	value &= ~ME_FIRMWARE_CS_WRITE;
	outl(value, register_base_control + PLX_ICR);

	// Download Xilinx firmware
	udelay(10);

	for (idx = 0; idx < fw->size; idx++) {
		outl(fw->data[idx], register_base_data);
#ifdef ME6000_v2_4
///     This checking only for board's version 2.4
		// Check if BUSY flag is set (low = ready, high = busy)
		if (inl(register_base_control + PLX_ICR) &
		    ME_FIRMWARE_BUSY_FLAG) {
			PERROR("Xilinx is still busy (idx = %d)\n", idx);
			release_firmware(fw);
			return -EIO;
		}
#endif //ME6000_v2_4
	}
	PDEBUG("Download finished. %d bytes written to PLX.\n", idx);

	// If done flag is high download was successful
	if (inl(register_base_control + PLX_ICR) & ME_FIRMWARE_DONE_FLAG) {
		PDEBUG("SUCCESS. Done flag is set.\n");
	} else {
		PERROR("FAILURE. DONE flag is not set.\n");
		release_firmware(fw);
		return -EIO;
	}

	// Set /CS and /WRITE
	value = inl(register_base_control + PLX_ICR);
	value |= ME_FIRMWARE_CS_WRITE;
	outl(value, register_base_control + PLX_ICR);

	PDEBUG("Enable interrupts on the PCI interface.\n");
	outl(ME_PLX_PCI_ACTIVATE, register_base_control + PLX_INTCSR);
	release_firmware(fw);

	return 0;
}