summaryrefslogtreecommitdiffstats
path: root/drivers/char/aspeed/ast_kcs.c
blob: 2eca42c761a1f9f881f27ad7e5838cc9331675c5 (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
/*
 * ASPEED AST2100/2050/2200/2150/2300 KCS controller driver
 *
 * (C) Copyright 2017 Raptor Engineering, LLC
 * (C) Copyright 2006-2009, American Megatrends Inc.
 *
 * Author : Timothy Pearson <tpearson@raptorengineering.com>
 *          Jothiram Selvam <jothirams@ami.com>
 *          Vinay Tandon <vinayt@ami.com>
 */

// #define DEBUG

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/errno.h>
#include <linux/ioport.h>

#include <asm/irq.h>
#include <asm/io.h>
#include <asm/uaccess.h>

#include <char/kcs/kcs.h>
#include <mach/ast_kcs.h>
#include <plat/regs-intr.h>

#define AST_KCS_DRIVER_NAME		"ast_kcs"

static void *ast_kcs_virt_base;
bool kcs_enabled_by_user = 0;

inline uint32_t ast_kcs_read_reg(uint32_t reg)
{
	return ioread32(ast_kcs_virt_base + reg);
}

inline void ast_kcs_write_reg(uint32_t data, uint32_t reg)
{
	iowrite32(data, ast_kcs_virt_base + reg);
}

inline uint32_t ast_lpcreset_read_reg(uint32_t reg)
{
	return ioread32(ast_kcs_virt_base + reg);
}

inline void ast_lpcreset_write_reg(uint32_t data, uint32_t reg)
{
	iowrite32(data, ast_kcs_virt_base + reg);
}

static void ast_kcs_enable_channel(void)
{
	uint32_t reg;

	reg = ast_kcs_read_reg(AST_LPC_HICR0);
	reg |= (AST_LPC_HICR0_LPC3E | AST_LPC_HICR0_LPC2E | AST_LPC_HICR0_LPC1E);
	ast_kcs_write_reg(reg, AST_LPC_HICR0);

#if defined(CONFIG_ARCH_AST2300) || defined(CONFIG_ARCH_AST2400)
	reg = ast_kcs_read_reg(AST_LPC_HICRB);
	reg |= AST_LPC_HICRB0_KCS4E;
	ast_kcs_write_reg(reg, AST_LPC_HICRB);
#endif
}

static void ast_kcs_disable_channel(void)
{
	uint32_t reg;

	reg = ast_kcs_read_reg(AST_LPC_HICR0);
	reg &= ~(AST_LPC_HICR0_LPC3E | AST_LPC_HICR0_LPC2E | AST_LPC_HICR0_LPC1E);
	ast_kcs_write_reg(reg, AST_LPC_HICR0);

#if defined(CONFIG_ARCH_AST2300) || defined(CONFIG_ARCH_AST2400)
	reg = ast_kcs_read_reg(AST_LPC_HICRB);
	reg &= ~(AST_LPC_HICRB0_KCS4E );
	ast_kcs_write_reg(reg, AST_LPC_HICRB);
#endif
}

static void ast_kcs_configure_io_port_addr(void)
{
	uint32_t reg;

	/* switch the access of LADR12 to LADR1 */
	reg = ast_kcs_read_reg(AST_LPC_HICR4);
	reg &= ~AST_LPC_HICR4_LADR12SEL;
	ast_kcs_write_reg(reg, AST_LPC_HICR4);

	mb();

	/* set I/O port address of channel 1*/
	ast_kcs_write_reg(AST_KCS_ADR1_HI, AST_LPC_LADR12H);
	ast_kcs_write_reg(AST_KCS_ADR1_LO, AST_LPC_LADR12L);

	mb();

	/* switch the access of LADR12 to LADR2 */
	reg = ast_kcs_read_reg(AST_LPC_HICR4);
	reg |= AST_LPC_HICR4_LADR12SEL;
	ast_kcs_write_reg(reg, AST_LPC_HICR4);

	mb();

	/* set I/O port address 2 */
	ast_kcs_write_reg(AST_KCS_ADR2_HI, AST_LPC_LADR12H);
	ast_kcs_write_reg(AST_KCS_ADR2_LO, AST_LPC_LADR12L);

	mb();

	/* enable KCS in channel 3 */
	reg = ast_kcs_read_reg(AST_LPC_HICR4);
	reg |= AST_LPC_HICR4_KCSENBL;
	ast_kcs_write_reg(reg, AST_LPC_HICR4);

	mb();

	/* set I/O port address of channel 3 */
	ast_kcs_write_reg(AST_KCS_ADR3_HI, AST_LPC_LADR3H);
	ast_kcs_write_reg(AST_KCS_ADR3_LO, AST_LPC_LADR3L);

	mb();

#if defined(CONFIG_ARCH_AST2300) || defined(CONFIG_ARCH_AST2400)
	/* enable KCS in channel 4 */
	reg = ast_kcs_read_reg(AST_LPC_HICRB);
	reg |= AST_LPC_HICRB0_KCS4E;
	ast_kcs_write_reg(reg, AST_LPC_HICRB);

	mb();

	/* set I/O port address of channel 4 */
	ast_kcs_write_reg(AST_KCS_ADR4, AST_LPC_LADR4);
#endif

}

static unsigned char ast_kcs_num_ch(void)
{
	return AST_KCS_CHANNEL_NUM;
}

static void ast_kcs_enable_interrupt(void)
{
	uint32_t reg;

	reg = ast_kcs_read_reg(AST_LPC_HICR2);
	reg |= (AST_LPC_HICR2_IBFIE1 | AST_LPC_HICR2_IBFIE2 | AST_LPC_HICR2_IBFIE3);
	ast_kcs_write_reg(reg, AST_LPC_HICR2);

#if defined(CONFIG_ARCH_AST2300) || defined(CONFIG_ARCH_AST2400)
	reg = ast_kcs_read_reg(AST_LPC_HICRB);
	reg |= (AST_LPC_HICRB0_KCS4INTE);
	ast_kcs_write_reg(reg, AST_LPC_HICRB);
#endif
}

static void ast_kcs_disable_interrupt(void)
{
	uint32_t reg;

	reg = ast_kcs_read_reg(AST_LPC_HICR2);
	reg &= ~(AST_LPC_HICR2_IBFIE1 | AST_LPC_HICR2_IBFIE2 | AST_LPC_HICR2_IBFIE3);
	ast_kcs_write_reg(reg, AST_LPC_HICR2);

#if defined(CONFIG_ARCH_AST2300) || defined(CONFIG_ARCH_AST2400)
	reg = ast_kcs_read_reg(AST_LPC_HICRB);
	reg &= ~(AST_LPC_HICRB0_KCS4INTE);
	ast_kcs_write_reg(reg, AST_LPC_HICRB);
#endif
}

static void ast_lpcreset_enable_interrupt(void)
{
	uint32_t reg;

	reg = ast_lpcreset_read_reg(AST_LPC_HICR2);
	reg |= AST_LPC_HICR2_ERRIE;
	ast_lpcreset_write_reg(reg, AST_LPC_HICR2);
}

static void ast_lpcreset_disable_interrupt(void)
{
	uint32_t reg;

	reg = ast_lpcreset_read_reg(AST_LPC_HICR2);
	reg &= ~(AST_LPC_HICR2_ERRIE);
	ast_lpcreset_write_reg(reg, AST_LPC_HICR2);
}

static void ast_kcs_read_status(u8 channel, u8 *status)
{
	if (channel == 3)
	{
#if defined(CONFIG_ARCH_AST2300) || defined(CONFIG_ARCH_AST2400)
		*status = (uint8_t) ast_kcs_read_reg(AST_LPC_STR4);
#endif
	}
	else
		*status = (uint8_t) ast_kcs_read_reg(AST_LPC_STR_CH(channel));
}

static void ast_kcs_write_status(u8 channel, u8 status)
{
	if (channel == 3)
	{
#if defined(CONFIG_ARCH_AST2300) || defined(CONFIG_ARCH_AST2400)
		ast_kcs_write_reg(status, AST_LPC_STR4);
#endif
	}
	else
		ast_kcs_write_reg(status, AST_LPC_STR_CH(channel));

}

static void ast_kcs_read_command(u8 channel, u8 *command)
{
	if (channel == 3)
	{
#if defined(CONFIG_ARCH_AST2300) || defined(CONFIG_ARCH_AST2400)
		*command = (uint8_t) ast_kcs_read_reg(AST_LPC_IDR4);
#endif
	}
	else
		*command = (uint8_t) ast_kcs_read_reg(AST_LPC_IDR_CH(channel));
}

static void ast_kcs_read_data(u8 channel, u8 *data)
{
	if(channel == 3)
	{
#if defined(CONFIG_ARCH_AST2300) || defined(CONFIG_ARCH_AST2400)
		*data = (uint8_t) ast_kcs_read_reg(AST_LPC_IDR4);
#endif
	}
	else
		*data = (uint8_t) ast_kcs_read_reg(AST_LPC_IDR_CH(channel));
}

static void ast_kcs_write_data(u8 channel, u8 data)
{
	if (channel == 3)
	{
#if defined(CONFIG_ARCH_AST2300) || defined(CONFIG_ARCH_AST2400)
		ast_kcs_write_reg(data, AST_LPC_ODR4);
#endif
	}
	else
		ast_kcs_write_reg(data, AST_LPC_ODR_CH(channel));
}

void
ast_kcs_interrupt_enable_user (void)
{
	kcs_enabled_by_user = 1;
	ast_kcs_enable_interrupt();
}

void
ast_kcs_interrupt_disable_user (void)
{
	kcs_enabled_by_user = 0;
	ast_kcs_disable_interrupt();
}

static kcs_driver_operations_t ast_kcs_hw_ops = {
	.num_kcs_ch = ast_kcs_num_ch,
	.enable_kcs_interrupt = ast_kcs_enable_interrupt,
	.disable_kcs_interrupt = ast_kcs_disable_interrupt,
	.read_kcs_status = ast_kcs_read_status,
	.write_kcs_status = ast_kcs_write_status,
	.read_kcs_command = ast_kcs_read_command,
	.read_kcs_data_in = ast_kcs_read_data,
	.write_kcs_data_out = ast_kcs_write_data,
	.kcs_interrupt_enable_user = ast_kcs_interrupt_enable_user,
	.kcs_interrupt_disable_user = ast_kcs_interrupt_disable_user
};

static irqreturn_t ast_kcs_irq_handler(int irq, void *dev_id)
{
	uint32_t reg;
	int ch;
	int ret;
	uint8_t status;
	int handled;

	reg = ast_kcs_read_reg(AST_LPC_HICR6);


	if (reg & (AST_LPC_HICR6_SNP0_STR | AST_LPC_HICR6_SNP1_STR)) { /* snoop interrupt is occured */
		printk(KERN_WARNING "ast_kcs: Unhandled snoop request!\n");
		return IRQ_NONE;
	}

	reg = ast_kcs_read_reg(AST_LPC_HICR2);

	if (reg & AST_LPC_HICR2_LRST) {
		/* clear interrupt flag */
		reg &= ~(AST_LPC_HICR2_LRST);
		ast_lpcreset_write_reg(reg, AST_LPC_HICR2);

		// FIXME
		// How to signal reset request to userspace application / kernel kcs state machine?
		// Ignore reset for now...
		printk(KERN_DEBUG "LPC RESET [IGNORED]\n");
		handled = 1;
	}

	if (reg & AST_LPC_HICR2_SDWN) {
		/* clear interrupt flag */
		reg &= ~AST_LPC_HICR2_SDWN;
		ast_lpcreset_write_reg(reg, AST_LPC_HICR2);
		printk(KERN_DEBUG "LPC SWDN\n");
		handled = 1;
	}

	if (reg & AST_LPC_HICR2_ABRT) {
		/* clear interrupt flag */
		reg &= ~AST_LPC_HICR2_ABRT;
		ast_lpcreset_write_reg(reg, AST_LPC_HICR2);
		printk(KERN_DEBUG "LPC ABORT\n");
		handled = 1;
	}

	for (ch = 0; ch < AST_KCS_CHANNEL_NUM; ch ++) {
		ast_kcs_read_status(ch, &status);
		if (status & 0x02) { /* Command or Data_In register has been written by system-side software */
			handled = 1;
			ret = process_kcs_intr(ch);
			if ((ret != 0) && (ret != -ENXIO)) {
				printk(KERN_WARNING "KCS core IRQ handler failed\n");
			}
		}
		/* when the KCS core IRQ handler reads IDR, IDF is cleared automatically */
	}

	return (handled == 1) ? IRQ_HANDLED : IRQ_NONE;
}

static void ast_kcs_init_hw(void)
{
	uint32_t reg;
	int ch;
	u8 ch_num, status;

#ifdef SOC_AST2300
	reg = ast_kcs_read_reg(AST_LPC_HICR5);
	reg &= ~(0x00000500); /* clear bit 10 & 8 to disable LPC flash cycle */
	ast_kcs_write_reg(reg, AST_LPC_HICR5);
#endif

	ast_kcs_disable_interrupt();
	ast_kcs_disable_channel();

	ast_kcs_configure_io_port_addr();

	/* clear OBF */
	for (ch = 0; ch < AST_KCS_CHANNEL_NUM; ch ++) {
		if(ch == 3)
		{
#ifdef SOC_AST2300
			reg = ast_kcs_read_reg(AST_LPC_STR4);
			reg &= ~AST_LPC_STR_OBFA;
			ast_kcs_write_reg(reg, AST_LPC_STR4);
#endif
		}
		else
		{
			reg = ast_kcs_read_reg(AST_LPC_STR_CH(ch));
			reg &= ~AST_LPC_STR_OBFA;
			ast_kcs_write_reg(reg, AST_LPC_STR_CH(ch));
		}
	}
	for (ch_num = 0; ch_num < AST_KCS_CHANNEL_NUM; ++ch_num)
	{
		ast_kcs_read_status (ch_num, &status);
		status = status | ERROR_STATE;
		ast_kcs_write_status (ch_num, status);
	}

	ast_kcs_enable_channel();
	// ast_kcs_enable_interrupt();
}

static int ast_kcs_probe(struct platform_device *pdev) {
	int ret = 0;
	uint32_t reg;
	struct resource *res0;

	dev_dbg(&pdev->dev, "ast_kcs_probe() \n\n\n");

	ret = kcs_init(&ast_kcs_hw_ops, pdev->id, THIS_MODULE);
	if (ret) {
		printk(KERN_WARNING "%s: initialization of KCS library failed\n", AST_KCS_DRIVER_NAME);
		return ret;
	}

	res0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res0) {
		dev_err(&pdev->dev, "cannot get IORESOURCE_MEM 0\n");
		goto err_no_io_res;
	}

	ast_kcs_virt_base = ioremap(res0->start, resource_size(res0));
	if (!ast_kcs_virt_base) {
		printk(KERN_WARNING "%s: ioremap failed\n", AST_KCS_DRIVER_NAME);
		ret = -ENOMEM;
		goto err_no_io_res;
	}

	/* clear interrupt status and disable interrupt of KCS and LPC-reset */
	reg = ast_lpcreset_read_reg(AST_LPC_HICR2);
	reg &= ~(AST_LPC_HICR2_LRST | AST_LPC_HICR2_SDWN | AST_LPC_HICR2_ABRT | AST_LPC_HICR2_IBFIE1 | AST_LPC_HICR2_IBFIE2 | AST_LPC_HICR2_IBFIE3 | AST_LPC_HICR2_ERRIE);
	ast_lpcreset_write_reg(reg, AST_LPC_HICR2);

	/* disable interrupt of snoop */
	reg = ast_lpcreset_read_reg(AST_LPC_HICR5);
	reg &= ~(AST_LPC_HICR5_SNP0_EN | AST_LPC_HICR5_SNP0_ENINT | AST_LPC_HICR5_SNP1_EN | AST_LPC_HICR5_SNP1_ENINT);
	ast_lpcreset_write_reg(reg, AST_LPC_HICR5);

	/* clear interrupt status of snoop */
	reg = ast_lpcreset_read_reg(AST_LPC_HICR6);
	reg |= (AST_LPC_HICR6_SNP0_STR | AST_LPC_HICR6_SNP1_STR);
	ast_lpcreset_write_reg(reg, AST_LPC_HICR6);

	IRQ_SET_LEVEL_TRIGGER(0, AST_KCS_IRQ);
	IRQ_SET_HIGH_LEVEL(0, AST_KCS_IRQ);

	ret = request_irq(AST_KCS_IRQ, ast_kcs_irq_handler, IRQF_SHARED, AST_KCS_DRIVER_NAME, ast_kcs_virt_base);
	if (ret) {
		printk(KERN_WARNING "%s: request irq failed\n", AST_KCS_DRIVER_NAME);
		goto out_iomap;
	}

	ast_kcs_init_hw();

	return 0;

out_iomap:
	iounmap(ast_kcs_virt_base);
err_no_io_res:
	kcs_exit();

	return ret;
}

static int
ast_kcs_remove(struct platform_device *pdev)
{
	struct resource *res0;

	dev_dbg(&pdev->dev, "ast_kcs_remove()\n");

	res0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	release_mem_region(res0->start, res0->end - res0->start + 1);
	iounmap(ast_kcs_virt_base);

	platform_set_drvdata(pdev, NULL);
	kcs_exit();
	return 0;
}


#ifdef CONFIG_PM
static int
ast_kcs_suspend(struct platform_device *pdev, pm_message_t msg)
{
	return 0;
}

static int
ast_kcs_resume(struct platform_device *pdev)
{
	return 0;
}
#else
#define ast_kcs_suspend NULL
#define ast_kcs_resume NULL
#endif


static struct platform_driver ast_kcs_driver = {
        .probe = ast_kcs_probe,
        .remove = ast_kcs_remove,
        .suspend = ast_kcs_suspend,
        .resume = ast_kcs_resume,
        .driver = {
                .name = "ast-kcs",
                .owner = THIS_MODULE,
        },
};

static int ast_kcs_module_init(void)
{
	platform_driver_register(&ast_kcs_driver);

	return 0;
}

static void ast_kcs_module_exit(void)
{
	free_irq(AST_KCS_IRQ, ast_kcs_virt_base);
	iounmap(ast_kcs_virt_base);
	platform_driver_unregister(&ast_kcs_driver);
	kcs_exit();
}

module_init(ast_kcs_module_init);
module_exit(ast_kcs_module_exit);

MODULE_AUTHOR("American Megatrends Inc.");
MODULE_DESCRIPTION("AST2100/2050/2200/2150/2300 KCS controller driver");
MODULE_LICENSE("GPL");

OpenPOWER on IntegriCloud