aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/adm5120/files-3.18/arch/mips/adm5120/common/platform.c
blob: bdbfbcbae5121bad0054b70f0439dcc3415b05c8 (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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 *  ADM5120 generic platform devices
 *
 *  Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 *
 */

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/export.h>

#include <asm/bootinfo.h>

#include <asm/mach-adm5120/adm5120_defs.h>
#include <asm/mach-adm5120/adm5120_info.h>
#include <asm/mach-adm5120/adm5120_switch.h>
#include <asm/mach-adm5120/adm5120_nand.h>
#include <asm/mach-adm5120/adm5120_platform.h>

#if 1
/*
 * TODO:remove global adm5120_eth* variables when the switch driver will be
 *	converted into a real platform driver
 */
unsigned int adm5120_eth_num_ports = 6;
EXPORT_SYMBOL_GPL(adm5120_eth_num_ports);

unsigned char adm5120_eth_macs[6][6] = {
	{'\00', 'A', 'D', 'M', '\x51', '\x20' },
	{'\00', 'A', 'D', 'M', '\x51', '\x21' },
	{'\00', 'A', 'D', 'M', '\x51', '\x22' },
	{'\00', 'A', 'D', 'M', '\x51', '\x23' },
	{'\00', 'A', 'D', 'M', '\x51', '\x24' },
	{'\00', 'A', 'D', 'M', '\x51', '\x25' }
};
EXPORT_SYMBOL_GPL(adm5120_eth_macs);

unsigned char adm5120_eth_vlans[6] = {
	0x41, 0x42, 0x44, 0x48, 0x50, 0x60
};
EXPORT_SYMBOL_GPL(adm5120_eth_vlans);
#endif

void __init adm5120_setup_eth_macs(u8 *mac_base)
{
	u32 t;
	int i, j;

	t = ((u32) mac_base[3] << 16) | ((u32) mac_base[4] << 8)
		| ((u32) mac_base[5]);

	for (i = 0; i < ARRAY_SIZE(adm5120_eth_macs); i++) {
		for (j = 0; j < 3; j++)
			adm5120_eth_macs[i][j] = mac_base[j];

		adm5120_eth_macs[i][3] = (t >> 16) & 0xff;
		adm5120_eth_macs[i][4] = (t >> 8) & 0xff;
		adm5120_eth_macs[i][5] = t & 0xff;

		t++;
	}
}

/*
 * Built-in ethernet switch
 */
struct resource adm5120_switch_resources[] = {
	[0] = {
		.start	= ADM5120_SWITCH_BASE,
		.end	= ADM5120_SWITCH_BASE+ADM5120_SWITCH_SIZE-1,
		.flags	= IORESOURCE_MEM,
	},
	[1] = {
		.start	= ADM5120_IRQ_SWITCH,
		.end	= ADM5120_IRQ_SWITCH,
		.flags	= IORESOURCE_IRQ,
	},
};

struct adm5120_switch_platform_data adm5120_switch_data;
struct platform_device adm5120_switch_device = {
	.name		= "adm5120-switch",
	.id		= -1,
	.num_resources	= ARRAY_SIZE(adm5120_switch_resources),
	.resource	= adm5120_switch_resources,
	.dev.platform_data = &adm5120_switch_data,
};

void __init adm5120_add_device_switch(unsigned num_ports, u8 *vlan_map)
{
	if (num_ports > 0)
		adm5120_eth_num_ports = num_ports;

	if (vlan_map)
		memcpy(adm5120_eth_vlans, vlan_map, sizeof(adm5120_eth_vlans));

	platform_device_register(&adm5120_switch_device);
}

/*
 * USB Host Controller
 */
struct resource adm5120_hcd_resources[] = {
	[0] = {
		.start	= ADM5120_USBC_BASE,
		.end	= ADM5120_USBC_BASE+ADM5120_USBC_SIZE-1,
		.flags	= IORESOURCE_MEM,
	},
	[1] = {
		.start	= ADM5120_IRQ_USBC,
		.end	= ADM5120_IRQ_USBC,
		.flags	= IORESOURCE_IRQ,
	},
};

static u64 adm5120_hcd_dma_mask = DMA_BIT_MASK(24);
struct platform_device adm5120_hcd_device = {
	.name		= "adm5120-hcd",
	.id		= -1,
	.num_resources	= ARRAY_SIZE(adm5120_hcd_resources),
	.resource	= adm5120_hcd_resources,
	.dev = {
		.dma_mask		= &adm5120_hcd_dma_mask,
		.coherent_dma_mask	= DMA_BIT_MASK(24),
	}
};

void __init adm5120_add_device_usb(void)
{
	platform_device_register(&adm5120_hcd_device);
}

/*
 * NOR flash devices
 */
struct adm5120_flash_platform_data adm5120_flash0_data;
struct platform_device adm5120_flash0_device =	{
	.name			= "adm5120-flash",
	.id			= 0,
	.dev.platform_data	= &adm5120_flash0_data,
};

struct adm5120_flash_platform_data adm5120_flash1_data;
struct platform_device adm5120_flash1_device =	{
	.name			= "adm5120-flash",
	.id			= 1,
	.dev.platform_data	= &adm5120_flash1_data,
};

void __init adm5120_add_device_flash(unsigned id)
{
	struct platform_device *pdev;

	switch (id) {
	case 0:
		pdev = &adm5120_flash0_device;
		break;
	case 1:
		pdev = &adm5120_flash1_device;
		break;
	default:
		pdev = NULL;
		break;
	}

	if (pdev)
		platform_device_register(pdev);
}

/*
 * built-in UARTs
 */
static void adm5120_uart_set_mctrl(struct amba_device *dev, void __iomem *base,
		unsigned int mctrl)
{
}

struct amba_pl010_data adm5120_uart0_data = {
	.set_mctrl = adm5120_uart_set_mctrl
};

struct amba_device adm5120_uart0_device = {
	.dev		= {
		.init_name = "apb:uart0",
		.platform_data = &adm5120_uart0_data,
	},
	.res		= {
		.start	= ADM5120_UART0_BASE,
		.end	= ADM5120_UART0_BASE + ADM5120_UART_SIZE - 1,
		.flags	= IORESOURCE_MEM,
	},
	.irq		= { ADM5120_IRQ_UART0, 0 },
	.periphid	= 0x0041010,
};

struct amba_pl010_data adm5120_uart1_data = {
	.set_mctrl = adm5120_uart_set_mctrl
};

struct amba_device adm5120_uart1_device = {
	.dev		= {
		.init_name = "apb:uart1",
		.platform_data = &adm5120_uart1_data,
	},
	.res		= {
		.start	= ADM5120_UART1_BASE,
		.end	= ADM5120_UART1_BASE + ADM5120_UART_SIZE - 1,
		.flags	= IORESOURCE_MEM,
	},
	.irq		= { ADM5120_IRQ_UART1, 0 },
	.periphid	= 0x0041010,
};

void __init adm5120_add_device_uart(unsigned id)
{
	struct amba_device *dev;

	switch (id) {
	case 0:
		dev = &adm5120_uart0_device;
		break;
	case 1:
		dev = &adm5120_uart1_device;
		break;
	default:
		dev = NULL;
		break;
	}

	if (dev)
		amba_device_register(dev, &iomem_resource);
}

/*
 * GPIO buttons
 */
void __init adm5120_register_gpio_buttons(int id,
					  unsigned poll_interval,
					  unsigned nbuttons,
					  struct gpio_keys_button *buttons)
{
	struct platform_device *pdev;
	struct gpio_keys_platform_data pdata;
	struct gpio_keys_button *p;
	int err;

	p = kmemdup(buttons, nbuttons * sizeof(*p), GFP_KERNEL);
	if (!p)
		return;

	pdev = platform_device_alloc("gpio-keys-polled", id);
	if (!pdev)
		goto err_free_buttons;

	memset(&pdata, 0, sizeof(pdata));
	pdata.poll_interval = poll_interval;
	pdata.nbuttons = nbuttons;
	pdata.buttons = p;

	err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
	if (err)
		goto err_put_pdev;

	err = platform_device_add(pdev);
	if (err)
		goto err_put_pdev;

	return;

err_put_pdev:
	platform_device_put(pdev);

err_free_buttons:
	kfree(p);
}

/*
 * GPIO LEDS
 */
struct gpio_led_platform_data adm5120_gpio_leds_data;
struct platform_device adm5120_gpio_leds_device = {
	.name		= "leds-gpio",
	.id		= -1,
	.dev.platform_data = &adm5120_gpio_leds_data,
};

void __init adm5120_add_device_gpio_leds(unsigned num_leds,
				    struct gpio_led *leds)
{
	struct gpio_led *p;

	p = kmalloc(num_leds * sizeof(*p), GFP_KERNEL);
	if (!p)
		return;

	memcpy(p, leds, num_leds * sizeof(*p));
	adm5120_gpio_leds_data.num_leds = num_leds;
	adm5120_gpio_leds_data.leds = p;

	platform_device_register(&adm5120_gpio_leds_device);
}

/*
 * NAND flash
 */
struct resource adm5120_nand_resources[] = {
	[0] = {
		.start	= ADM5120_NAND_BASE,
		.end	= ADM5120_NAND_BASE + ADM5120_NAND_SIZE-1,
		.flags	= IORESOURCE_MEM,
	},
};

static int adm5120_nand_ready(struct mtd_info *mtd)
{
	return ((adm5120_nand_get_status() & ADM5120_NAND_STATUS_READY) != 0);
}

static void adm5120_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
					unsigned int ctrl)
{
	if (ctrl & NAND_CTRL_CHANGE) {
		adm5120_nand_set_cle(ctrl & NAND_CLE);
		adm5120_nand_set_ale(ctrl & NAND_ALE);
		adm5120_nand_set_cen(ctrl & NAND_NCE);
	}

	if (cmd != NAND_CMD_NONE)
		NAND_WRITE_REG(NAND_REG_DATA, cmd);
}

void __init adm5120_add_device_nand(struct platform_nand_data *pdata)
{
	struct platform_device *pdev;
	int err;

	pdev = platform_device_alloc("gen_nand", -1);
	if (!pdev)
		goto err_out;

	err = platform_device_add_resources(pdev, adm5120_nand_resources,
					ARRAY_SIZE(adm5120_nand_resources));
	if (err)
		goto err_put;

	err = platform_device_add_data(pdev, pdata, sizeof(*pdata));
	if (err)
		goto err_put;

	pdata = pdev->dev.platform_data;
	pdata->ctrl.dev_ready = adm5120_nand_ready;
	pdata->ctrl.cmd_ctrl = adm5120_nand_cmd_ctrl;

	err = platform_device_add(pdev);
	if (err)
		goto err_put;

	return;

err_put:
	platform_device_put(pdev);
err_out:
	return;
}