summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/board_nvodm.c
blob: d9eefc00db3f315ba82920829fb9a80f93e6a457 (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
 * arch/arm/mach-tegra/board-nvodm.c
 *
 * Board registration for ODM-kit generic Tegra boards
 *
 * Copyright (c) 2009, NVIDIA Corporation.
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/platform_device.h>
#include <linux/synaptics_i2c_rmi.h>
#include <linux/i2c.h>
#include <linux/pm.h>
#include <linux/spi/spi.h>
#if CONFIG_KEYBOARD_GPIO
#include <linux/gpio_keys.h>
#include <linux/input.h>
#include "include/mach/gpio-names.h"
#endif

#include <asm/mach-types.h>
#include <asm/mach/arch.h>

#include <mach/board.h>
#include <mach/platform.h>
#include <mach/kbc.h>

#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>

#include <linux/io.h>
#include <linux/serial_8250.h>

#include <linux/delay.h>

#include "nvcommon.h"
#include "nvrm_init.h"
#include "nvrm_module.h"
#include "nvrm_interrupt.h"
#include "nvrm_pinmux.h"
#include "nvrm_power.h"
#include "nvrm_pmu.h"
#include "nvodm_query.h"
#include "nvodm_services.h"
#include "nvodm_sdio.h"
#include "nvodm_pmu.h"
#include "nvrm_gpio.h"
#include "mach/nvrm_linux.h"
#include "nvassert.h"
#include "nvodm_query_discovery.h"

#include <../../../drivers/staging/android/timed_output.h>

#if defined(CONFIG_USB_ANDROID) || defined(CONFIG_USB_ANDROID_MODULE)
#include <linux/usb/android.h>
#define SERIAL_NUMBER_STRING_LEN 16
#endif

extern struct sys_timer tegra_timer;
extern const char* tegra_boot_device;
extern void __init tegra_init_irq(void);
extern void __init tegra_map_common_io(void);

static struct platform_device nvrm_device =
{
    .name = "nvrm"
};

#if defined(CONFIG_USB_ANDROID) || defined(CONFIG_USB_ANDROID_MODULE)
static struct android_usb_platform_data android_usb_plat =
{
    .vendor_id = 0x0955,
    .product_id = 0x7000,
    .adb_product_id = 0x7100,
    .product_name = "ADB Composite Device",
    .manufacturer_name = "NVIDIA Corporation",
    .serial_number = "0000000000000000",
    .nluns = 1,
    .bulk_size = 16384,
};

static struct platform_device android_usb_device =
{
    .name = "android_usb",
    .dev =
    {
        .platform_data = &android_usb_plat,
    },
};
#endif

#ifdef CONFIG_TEGRA_NVEC_USER
static struct platform_device tegra_nvec =
{
    .name = "nvec",
    .id   = -1,
};
#endif

#if defined(CONFIG_USB_GADGETFS) || defined(CONFIG_USB_GADGETFS_MODULE)
static struct platform_device android_gadgetfs_device =
{
    .name = "gadgetfs",
    .id   = -1,
};
#endif

#if defined(CONFIG_BATTERY_TEGRA_ODM) || defined(CONFIG_TEGRA_BATTERY_NVEC)
static struct platform_device tegra_battery_device =
{
    .name = "tegra_battery",
    .id   = -1,
};
#endif

#ifdef CONFIG_MTD_NAND_TEGRA
static struct platform_device tegra_nand_device =
{
    .name = "tegra_nand",
    .id   = -1,
};
#endif


#if CONFIG_KEYBOARD_GPIO
static struct gpio_keys_button tegra_buttons[] = {
	{
		.gpio		= TEGRA_GPIO_PU5,
		.code		= SW_LID,
		.desc		= "LID",
		.active_low	= 0,
		.type           = EV_SW,
		.wakeup         = 1,
	}
};

static struct gpio_keys_platform_data tegra_button_data = {
	.buttons	= tegra_buttons,
	.nbuttons	= ARRAY_SIZE(tegra_buttons),
};

static struct platform_device tegra_button_device = {
	.name	= "gpio-keys",
	.id		= 3,
	.dev		= {
		.platform_data	= &tegra_button_data,
	}
};
#endif

#ifdef CONFIG_RTC_DRV_TEGRA_ODM
static struct platform_device tegra_rtc_device =
{
    .name = "tegra_rtc",
    .id   = -1,
};
#endif

#ifdef CONFIG_TOUCHSCREEN_TEGRA_ODM
static struct platform_device tegra_touch_device =
{
    .name = "tegra_touch",
    .id   = -1,
};
#endif

#ifdef CONFIG_INPUT_TEGRA_ODM_ACCEL
static struct platform_device tegra_accelerometer_device =
{
    .name = "tegra_accelerometer",
    .id   = -1,
};
#endif

#ifdef CONFIG_INPUT_TEGRA_ODM_SCROLL
static struct platform_device tegra_scrollwheel_device =
{
    .name = "tegra_scrollwheel",
    .id   = -1,
};
#endif

#ifdef CONFIG_TEGRA_ODM_VIBRATE
static struct timed_output_dev tegra_vibrator_device = {
    .name = "tegra_vibrator",
};
#endif

#ifdef CONFIG_TEGRA_ODM_RFKILL
static struct platform_device tegra_rfkill =
{
    .name = "tegra_rfkill",
    .id   = -1,
};
#endif


/*  FIXME:  NvBl uses 57.6k for FPGAs.  16x multiplier for UART baud rate
 *  should be chip-dependent. */
#define TARGET_BAUD_RATE 115200
static void __init NvGetUartClockRange(NvU32 BaudBitRate, NvU32 *pTargetClkKHz,
    NvU32 *pMinClkKHz, NvU32 *pMaxClkKHz)
{
    NvU32 TargetClkKHz = (BaudBitRate * 16) / 1000;
    //  DDK uses +- 5% tolerance.  We use +- 6.25% to remove the extra divides
    *pMinClkKHz = TargetClkKHz - (TargetClkKHz>>4);
    *pMaxClkKHz = TargetClkKHz + (TargetClkKHz>>4);
    *pTargetClkKHz = TargetClkKHz;
}

static void __init NvConfigDebugConsole(
    NvRmDeviceHandle hRm)
{
    NvOdmDebugConsole Console = NvOdmQueryDebugConsole();
    struct plat_serial8250_port *pUartDebugPort = NULL;
    struct platform_device *pDebugConsole = NULL;
    NvU32 ModId = 0;

    /*  The debug console uses the standard serial 8250 driver,
     *  rather than the power-managed Tegra UART driver */
    if (((NvU32)Console >= (NvU32)NvOdmDebugConsole_UartA) &&
        ((NvU32)Console <= (NvU32)NvOdmDebugConsole_UartE))
    {
        NvU32 Port = (NvU32)(Console - NvOdmDebugConsole_UartA);
        NvU32 Clk, TargetClk, MinClk, MaxClk, Temp;

        ModId = NVRM_MODULE_ID(NvRmModuleID_Uart, Port);

        if (NvRmSetModuleTristate(hRm, ModId, NV_FALSE)!=NvSuccess)
            goto exit_fail;

        NvGetUartClockRange(TARGET_BAUD_RATE, &TargetClk, &MinClk, &MaxClk);

        if (NvRmPowerModuleClockConfig(hRm, ModId, 0, MinClk, MaxClk,
                &TargetClk, 1, &Clk, 0)!=NvSuccess)
            goto cleanup_ts;

        if (NvRmPowerModuleClockControl(hRm, ModId, 0, NV_TRUE)!=NvSuccess)
            goto cleanup_ts;

        NvRmModuleReset(hRm, ModId);
        pDebugConsole = (struct platform_device *)
            NvOsAlloc(sizeof(struct platform_device));
        if (!pDebugConsole)
            goto cleanup;
        pUartDebugPort = (struct plat_serial8250_port *)
            NvOsAlloc(2*sizeof(struct plat_serial8250_port));
        if (!pUartDebugPort)
            goto cleanup;

        NvOsMemset(pUartDebugPort, 0, 2*sizeof(struct plat_serial8250_port));
        NvOsMemset(pDebugConsole, 0, sizeof(struct platform_device));
        pDebugConsole->name = "serial8250";
        pDebugConsole->id = PLAT8250_DEV_PLATFORM;

        NvRmModuleGetBaseAddress(hRm, ModId, &pUartDebugPort->mapbase, &Temp);
        pUartDebugPort->membase = IO_ADDRESS(pUartDebugPort->mapbase);
        pUartDebugPort->uartclk = Clk * 1000;
        pUartDebugPort->irq = NvRmGetIrqForLogicalInterrupt(hRm, ModId, 0);
        pUartDebugPort->iotype = UPIO_MEM32;
        pUartDebugPort->regshift = 2;
        pUartDebugPort->flags = UPF_BOOT_AUTOCONF;
        pDebugConsole->dev.platform_data = pUartDebugPort;
        if (platform_device_register(pDebugConsole))
        {
            NV_ASSERT(!"Unable to register debug console device");
        }
    }

    return;

 cleanup:
    if (pDebugConsole)
        NvOsFree(pDebugConsole);
    if (pUartDebugPort)
        NvOsFree(pUartDebugPort);
    (void) NvRmPowerModuleClockControl(hRm, ModId, 0, NV_FALSE);
 cleanup_ts:
    (void) NvRmSetModuleTristate(hRm, ModId, NV_TRUE);
  exit_fail:
    return;
}

void tegra_set_voltage(NvU64 guid, int on)
{
	u32 settling_time;
	const NvOdmPeripheralConnectivity *con = NULL;
	int i;

	con = NvOdmPeripheralGetGuid(guid);
	if (con == NULL)
		return;

	for (i = 0; i < con->NumAddress; i++) {
		if (con->AddressList[i].Interface != NvOdmIoModule_Vdd)
			continue;
		if (on) {
			NvRmPmuVddRailCapabilities rail;
			NvRmPmuGetCapabilities(s_hRmGlobal,
				con->AddressList[i].Address, &rail);
			NvRmPmuSetVoltage(s_hRmGlobal,
				con->AddressList[i].Address,
				rail.requestMilliVolts, &settling_time);
		} else
			NvRmPmuSetVoltage(s_hRmGlobal,
				con->AddressList[i].Address, NVODM_VOLTAGE_OFF,
				&settling_time);
		udelay(settling_time);
	}
}

extern void __init tegra_common_init(void);
extern void __init tegra_clk_init(void);
#ifdef CONFIG_TEGRA_DPRAM
extern void __init tegra_init_snor_controller(void);
#endif


static void tegra_system_power_off(void)
{
	tegra_set_voltage(NV_VDD_SoC_ODM_ID, 0);
}

#if !(defined(CONFIG_ENC28J60) && defined(CONFIG_SPI_TEGRA))
#define register_enc28j60() do {} while (0)
#else
static struct spi_board_info tegra_spi_devices[] __initdata = {
    {
        .modalias = "enc28j60",
        .bus_num = 1,
        .chip_select = 0,
        .mode = SPI_MODE_0,
        .max_speed_hz = 18000000,
        .platform_data = NULL,
        .irq = 0,
    },
};

static void __init register_enc28j60(void)
{
    NvError err;
    NvRmGpioPinHandle hPin;
    NvU32 irq;
    NvU32 instance = 0xFFFF;
    NvU32 cs = 0xFFF;
    NvU32 pin = 0xFFFF, port = 0xFFFF;
    const NvOdmPeripheralConnectivity *pConnectivity = NULL;
    int i;
    const NvOdmQuerySpiDeviceInfo *pSpiDeviceInfo;

    pConnectivity =
        NvOdmPeripheralGetGuid(NV_ODM_GUID('e','n','c','2','8','j','6','0'));
    if (!pConnectivity)
        return;

    for (i = 0; i < pConnectivity->NumAddress; i++)
    {
        switch (pConnectivity->AddressList[i].Interface)
        {
            case NvOdmIoModule_Spi:
                instance = pConnectivity->AddressList[i].Instance;
                cs = pConnectivity->AddressList[i].Address;
                break;
            case NvOdmIoModule_Gpio:
                port = pConnectivity->AddressList[i].Instance;
                pin = pConnectivity->AddressList[i].Address;
                break;
            default:
                break;
        }
    }

    /* SPI ethernet driver needs one SPI info and a gpio for interrupt */
    if (instance == 0xffff || cs == 0xffff || port == 0xFFFF || pin == 0xFFFF)
        return;

    /* Check if the SPI is configured as a master for this instance
     * If it it not, don't register the device.
     * */
    pSpiDeviceInfo = NvOdmQuerySpiGetDeviceInfo(NvOdmIoModule_Spi, instance,
	cs);
    if (pSpiDeviceInfo && pSpiDeviceInfo->IsSlave)
        return;

    err = NvRmGpioAcquirePinHandle(s_hGpioGlobal, port, pin, &hPin);
    if (err)
    {
        return;
    }
    NvRmGpioConfigPins(s_hGpioGlobal, &hPin, 1,
        NvRmGpioPinMode_InputInterruptFallingEdge);
    NvRmGpioGetIrqs(s_hRmGlobal, &hPin, &irq, 1);

    printk("Enabled SPI driver\n");

    tegra_spi_devices[0].irq = irq;
    /* FIXME, instance need not be same as bus number. */
    tegra_spi_devices[0].bus_num = instance;
    tegra_spi_devices[0].chip_select = cs;
    spi_register_board_info(tegra_spi_devices, ARRAY_SIZE(tegra_spi_devices));
}
#endif


#ifndef CONFIG_KEYBOARD_TEGRA
#define kbc_init() do { } while (0)

#else
extern struct tegra_kbc_plat *tegra_kbc_odm_to_plat(void);

static void kbc_init(void)
{
    struct platform_device *dev = NULL;
    struct tegra_kbc_plat *pdata = NULL;
    static struct resource res[2];

    dev = platform_device_alloc("tegra_kbc", -1);
    if (!dev)
        goto fail;

    pdata = tegra_kbc_odm_to_plat();
    if (!pdata) {
        pr_err("tegra_kbc ODM kit to platform data translation failed\n");
        goto fail;
    }

    res[0].flags = IORESOURCE_MEM;
    res[0].start = tegra_get_module_inst_base("kbc", 0);
    res[0].end = (res[0].start + tegra_get_module_inst_size("kbc", 0));
    res[1].flags = IORESOURCE_IRQ;
    res[1].start = res[1].end =
        tegra_get_module_inst_irq("kbc", 0, 0);

    if ((res[0].end <= res[0].start) || (res[1].start == NO_IRQ)) {
        pr_err("no tegra_kbc module present\n");
        goto fail;
    }

    if (platform_device_add_resources(dev, res, ARRAY_SIZE(res))) {
        pr_err("failed to add resources to tegra_kbc device\n");
        goto fail;
    }

    if (platform_device_add_data(dev, pdata, sizeof(*pdata))) {
        pr_err("failed to add platform data to tegra_kbc device\n");
        goto fail;
    }

    /* fixme: something about wakeup irq, here */

    if (platform_device_add(dev)) {
        pr_err("failed to add tegra_kbc device\n");
        goto fail;
    }

    return;

fail:
    if (dev)
        platform_device_del(dev);

        kfree(pdata);
    return;
}
#endif

static struct spi_board_info tegra_spi_ipc_devices[] __initdata =
{
    {
        .modalias = "spi_ipc",
        .bus_num = 0,
        .chip_select = 0,
        .mode = SPI_MODE_1,
        .max_speed_hz = 12000000,
        .platform_data = NULL,
        .irq = 0,
    },
};

static void __init register_spi_ipc_devices(void)
{
    NvError err;
    NvU32 irq;
    NvU32 instance = 0xFFFF;
    NvU32 cs = 0xFFFF;
    const NvOdmPeripheralConnectivity *pConnectivity = NULL;
    int i;

    pConnectivity =
        NvOdmPeripheralGetGuid(NV_ODM_GUID('s','p','i',' ','_','i','p','c'));
    if (!pConnectivity)
        return;

    for (i = 0; i < pConnectivity->NumAddress; i++)
    {
        switch (pConnectivity->AddressList[i].Interface)
        {
            case NvOdmIoModule_Spi:
                instance = pConnectivity->AddressList[i].Instance;
                cs = pConnectivity->AddressList[i].Address;
                break;
            default:
                break;
        }
    }

    /* Infineon SPI Protocol driver needs one SPI info and a gpio for interrupt */
    if (instance == 0xffff || cs == 0xffff)
        return;

    printk("Enabled Infineon IPC Protocol  driver\n");

    tegra_spi_ipc_devices[0].bus_num = instance;
    tegra_spi_ipc_devices[0].chip_select = cs;
    spi_register_board_info(tegra_spi_ipc_devices, ARRAY_SIZE(tegra_spi_ipc_devices));
}


static void __init tegra_machine_init(void)
{
#if defined(CONFIG_USB_ANDROID) || defined(CONFIG_USB_ANDROID_MODULE)
    NvU32 serial_number[2] = {0};
#endif

    (void) platform_device_register(&nvrm_device);

    tegra_common_init();
    tegra_clk_init();
    NvConfigDebugConsole(s_hRmGlobal);

#ifdef CONFIG_TEGRA_DPRAM
    tegra_init_snor_controller();
#endif

#ifdef CONFIG_TEGRA_NVEC_USER
    (void) platform_device_register(&tegra_nvec);
#endif

#ifdef CONFIG_TOUCHSCREEN_TEGRA_ODM
    (void) platform_device_register(&tegra_touch_device);
#endif

#ifdef CONFIG_INPUT_TEGRA_ODM_ACCEL
    (void) platform_device_register(&tegra_accelerometer_device);
#endif

#ifdef CONFIG_TEGRA_ODM_VIBRATE
    (void) timed_output_dev_register(&tegra_vibrator_device);
#endif

    register_enc28j60();
    register_spi_ipc_devices();

    /* register the devices */
#ifdef CONFIG_MTD_NAND_TEGRA
    if (tegra_boot_device && !NvOsMemcmp(tegra_boot_device, "nand", 4))
        (void) platform_device_register(&tegra_nand_device);
#endif

#ifdef CONFIG_RTC_DRV_TEGRA_ODM
    (void) platform_device_register(&tegra_rtc_device);
#endif

    kbc_init();

#if defined(CONFIG_BATTERY_TEGRA_ODM) || defined(CONFIG_TEGRA_BATTERY_NVEC)
    (void) platform_device_register(&tegra_battery_device);
#endif

#if defined(CONFIG_USB_ANDROID) || defined(CONFIG_USB_ANDROID_MODULE)
    // get the board specific unique ID
    (void) NvRmQueryChipUniqueId(
                s_hRmGlobal,
                sizeof(NvU32) * 2,
                (void *)serial_number);

    snprintf(android_usb_plat.serial_number,
             SERIAL_NUMBER_STRING_LEN,
             "%08X%08X",
             serial_number[1],
             serial_number[0]);
    android_usb_device.dev.platform_data = &android_usb_plat;
    (void) platform_device_register(&android_usb_device);
#endif
#if defined(CONFIG_USB_GADGETFS) || defined(CONFIG_USB_GADGETFS_MODULE)
    (void) platform_device_register(&android_gadgetfs_device);
#endif

#ifdef CONFIG_INPUT_TEGRA_ODM_SCROLL
    (void) platform_device_register(&tegra_scrollwheel_device);
#endif


#ifdef CONFIG_TEGRA_ODM_RFKILL
    (void) platform_device_register(&tegra_rfkill);
#endif

#ifdef CONFIG_TEGRA_PCI
	tegra_set_voltage( NV_VDD_PEX_CLK_ODM_ID, 1);
#else
	tegra_set_voltage( NV_VDD_PEX_CLK_ODM_ID, 0);
#endif

#if CONFIG_KEYBOARD_GPIO
	platform_device_register(&tegra_button_device);
#endif

	pm_power_off = tegra_system_power_off;
}

MACHINE_START(TEGRA_GENERIC, "Tegra generic")

    .boot_params  = 0x00000100,
    .map_io       = tegra_map_common_io,
    .init_irq     = tegra_init_irq,
    .init_machine = tegra_machine_init,
    .timer        = &tegra_timer,

MACHINE_END