block_atsamx70_gmac.c 5.81 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
/*
	Copyright Jeroen Vreeken (jeroen@vreeken.net), 2018

	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 3 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, see <http://www.gnu.org/licenses/>.

 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <controller/controller_block.h>
#include <controller/controller_bus.h>
#include <controller/controller_mem.h>
#include <controller/controller_sample.h>
#include <log/log.h>

#include <packet/packet.h>
#include <packet/packet_bus.h>

#include <samx70.h>
#include <pio/samx70.h>
#include <ioport.h>
#include <gmac.h>

#define TX_Q_SIZE	4

struct controller_block_private {
	struct controller_bus *bus;
	
	void *rx_packet;
	void *tx_packet;
	size_t rx_size;
	size_t tx_size;
	
	struct gmac_state gmac;
	struct gmac_tx_buffer_descriptor tx_q[TX_Q_SIZE];
	char tx_buffer[TX_Q_SIZE][1600];
};

static void atsamx70_gmac_tx_calculate(struct controller_block *packet_tx)
{
	struct controller_block_private *priv = packet_tx->private;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
56
	((char*)priv->tx_packet)[11]++;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
	
	gmac_tx(&priv->gmac, priv->tx_packet, priv->tx_size);
}

static void packet_sample_start(void *arg)
{
	struct controller_block *packet = arg;
	struct controller_bus *bus = packet->private->bus;
	size_t header_size = 14;
	
	packet->private->tx_size = bus->private->tx_end_offset + header_size;
	packet->private->rx_size = bus->private->rx_end_offset + header_size;
	
	packet->private->tx_packet = controller_mem_calloc(
	    CONTROLLER_MEM_PERIODIC_READ | CONTROLLER_MEM_PERIODIC_WRITE,
	    packet->private->tx_size, 1);

	packet->private->rx_packet = controller_mem_calloc(
	    CONTROLLER_MEM_PERIODIC_READ | CONTROLLER_MEM_PERIODIC_WRITE,
	    packet->private->rx_size, 1);

	char *tx_start = packet->private->tx_packet;
	char *rx_start = packet->private->rx_packet;
	
	memcpy(tx_start + 0, (char[]){0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 6);
	memcpy(tx_start + 6, (char[]){0x01, 0x02, 0x03, 0x04, 0x05, 0x06}, 6);
	tx_start[12] = 0x44;
	tx_start[13] = 0x4d;
	
	tx_start += header_size;
	rx_start += header_size;

	packet_client_callbacks(bus, rx_start, tx_start);
}

static enum controller_bus_state atsamx70_gmac_poll(struct controller_bus *bus)
{
	struct gmac_state *gmac = &bus->owner->private->gmac;
	
	uint32_t nsr = gmac_network_status_get(gmac);
	uint32_t tsr = gmac_transmit_status_get(gmac);
	uint32_t isr = gmac_interrupt_status_get(gmac);
	
	log_send(LOG_T_DEBUG, "%s: NSR: 0x%08"PRIx32"  TSR: 0x%08"PRIx32"  ISR: 0x%08"PRIx32,
	   bus->owner->name, nsr, tsr, isr);
	if (tsr) {
		gmac_transmit_status_set(gmac, tsr);
	}

	return CONTROLLER_BUS_STATE_OK;
}

static int atsamx70_gmac_recover(struct controller_bus *bus)
{
	return 0;
}

static struct controller_block * block_atsamx70_gmac_create(char *name, int argc,
    va_list val)
{
	struct controller_block *packet;
	struct controller_bus *bus;
	char *name_tx;
	struct controller_block *packet_tx;
	
	asprintf(&name_tx, "%s_tx", name);
	if (!name_tx)
		return NULL;
	
	packet = controller_block_alloc("atsamx70_gmac", name, sizeof(struct controller_block_private));
	if (!packet)
		goto err_block;
	packet_tx = controller_block_alloc("atsamx70_gmac_tx", name_tx, 0);
	if (!packet)
		goto err_block_tx;
	packet_tx->private = packet->private;
	free(name_tx);
	name_tx = NULL;

	bus = packet_bus_create(name, packet);
	if (!bus)
		goto err_bus;
	controller_bus_output_set(bus, packet_tx);
	controller_bus_input_set(bus, packet);

	controller_bus_poll_set(bus, atsamx70_gmac_poll, atsamx70_gmac_recover);

	packet->private->bus = bus;

	packet_tx->calculate = atsamx70_gmac_tx_calculate;

	if (controller_block_add(packet))
		goto err_add;

	if (controller_block_add(packet_tx))
		goto err_add;

	controller_sample_start_hook(packet_sample_start, packet);

	/* RMII pins: 
		GTXCK	PD0,A
		GTXEN	PD1,A
		GTX0	PD2,A
		GTX1	PD3,A
		GRXDV	PD4,A
		GRX0	PD5,A
		GRX1	PD6,A
		GRXER	PD7,A
		GMDC	PD8,A
		GMDIO	PD9,A
		same70 xplained:
		RESET	PC10
		INT	PA14
	 */
	ioport_disable_pin(PIO_GTXCK_IDX);
	ioport_set_pin_mode(PIO_GTXCK_IDX, IOPORT_MODE_GTXCK);
	ioport_disable_pin(PIO_GTXEN_IDX);
	ioport_set_pin_mode(PIO_GTXEN_IDX, IOPORT_MODE_GTXEN);
	ioport_disable_pin(PIO_GTX0_IDX);
	ioport_set_pin_mode(PIO_GTX0_IDX, IOPORT_MODE_GTX0);
	ioport_disable_pin(PIO_GTX1_IDX);
	ioport_set_pin_mode(PIO_GTX1_IDX, IOPORT_MODE_GTX1);
	ioport_disable_pin(PIO_GRXDV_IDX);
	ioport_set_pin_mode(PIO_GRXDV_IDX, IOPORT_MODE_GRX0);
	ioport_disable_pin(PIO_GRX0_IDX);
	ioport_set_pin_mode(PIO_GRX1_IDX, IOPORT_MODE_GRX1);
	ioport_disable_pin(PIO_GRXER_IDX);
	ioport_set_pin_mode(PIO_GRXER_IDX, IOPORT_MODE_GRXER);
	ioport_disable_pin(PIO_GMDC_IDX);
	ioport_set_pin_mode(PIO_GMDC_IDX, IOPORT_MODE_GMDC);
	ioport_disable_pin(PIO_GMDIO_IDX);
	ioport_set_pin_mode(PIO_GMDIO_IDX, IOPORT_MODE_GMDIO);

	struct gmac_state *gmac = &packet->private->gmac;
	
	log_send(LOG_T_DEBUG, "gmac: %p, tx_q: %p", gmac, packet->private->tx_q);
	int i;
	for (i = 0; i < TX_Q_SIZE; i++) {
		packet->private->tx_q[i].buffer = packet->private->tx_buffer[i];
	}
	gmac_init(gmac, packet->private->tx_q, TX_Q_SIZE);

	gmac_network_configuration_set(gmac,
	    GMAC_NCFGR_SPD | GMAC_NCFGR_FD | GMAC_NCFGR_DBW(0) |
	    GMAC_NCFGR_CLK_MCK_64 | GMAC_NCFGR_MAXFS | GMAC_NCFGR_PEN |
	    GMAC_NCFGR_RFCS);

	return packet;

err_add:
err_bus:
err_block_tx:
	controller_block_free(packet);
err_block:
	free(name_tx);
	return NULL;
}

BLOCK_CREATE(atsamx70_gmac) = {
	.create = block_atsamx70_gmac_create,
	.args = { NULL },
};