summaryrefslogtreecommitdiffstats
path: root/drivers/serial/serial_ns16550.c
blob: 7d22873835bc0379f504496d414fd4ee5ec0a51e (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
/**
 * @file
 * @brief NS16550 Driver implementation
 *
 * FileName: drivers/serial/serial_ns16550.c
 *
 * NS16550 support
 * Modified from u-boot drivers/serial.c and drivers/ns16550.c
 * originally from linux source (arch/ppc/boot/ns16550.c)
 * modified to use CFG_ISA_MEM and new defines
 */
/*
 * (C) Copyright 2008
 * Texas Instruments, <www.ti.com>
 * Nishanth Menon <x0nishan@ti.com>
 *
 * (C) Copyright 2000
 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * 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.
 *
 */

#include <common.h>
#include <driver.h>
#include <init.h>
#include <errno.h>
#include <malloc.h>
#include <io.h>
#include <linux/clk.h>

#include "serial_ns16550.h"
#include <ns16550.h>

struct ns16550_priv {
	struct console_device cdev;
	struct NS16550_plat plat;
	int access_width;
	int mmio;
	struct clk *clk;
	uint32_t fcrval;
};

static inline struct ns16550_priv *to_ns16550_priv(struct console_device *cdev)
{
	return container_of(cdev, struct ns16550_priv, cdev);
}

struct ns16550_drvdata {
	void (*init_port)(struct console_device *cdev);
	const char *linux_console_name;
};

/**
 * @brief read system i/o (byte)
 * @param[in] addr address to read
 * @param[in] mmio memory i/o space or i/o port space
 */
static inline uint8_t ns16550_sys_readb(void __iomem *addr, int mmio)
{
	if (mmio)
		return readb(addr);
	else
		return (uint8_t) inb((int) addr);
}

/**
 * @brief read system i/o (word)
 * @param[in] addr address to read
 * @param[in] mmio memory i/o space or i/o port space
 */
static inline uint16_t ns16550_sys_readw(void __iomem *addr, int mmio)
{
	if (mmio)
		return readw(addr);
	else
		return (uint16_t) inw((int) addr);
}

/**
 * @brief read system i/o (dword)
 * @param[in] addr address to read
 * @param[in] mmio memory i/o space or i/o port space
 */
static inline uint32_t ns16550_sys_readl(void __iomem *addr, int mmio)
{
	if (mmio)
		return readl(addr);
	else
		return (uint32_t) inl((int) addr);
}

/**
 * @brief write system i/o (byte)
 * @param[in] val data to write
 * @param[in] addr address to write to
 * @param[in] mmio memory i/o space or i/o port space
 */
static inline void ns16550_sys_writeb(uint8_t val, void __iomem *addr,
				      int mmio)
{
	if (mmio)
		writeb(val, addr);
	else
		outb(val, (int) addr);
}

/**
 * @brief read system i/o (word)
 * @param[in] val data to write
 * @param[in] addr address to write to
 * @param[in] mmio memory i/o space or i/o port space
 */
static inline void ns16550_sys_writew(uint16_t val, void __iomem *addr,
				      int mmio)
{
	if (mmio)
		writew(val, addr);
	else
		outw(val, (int) addr);
}

/**
 * @brief read system i/o (dword)
 * @param[in] val data to write
 * @param[in] addr address to write to
 * @param[in] mmio memory i/o space or i/o port space
 */
static inline void ns16550_sys_writel(uint32_t val, void __iomem *addr,
				      int mmio)
{
	if (mmio)
		writel(val, addr);
	else
		outl(val, (int) addr);
}

/**
 * @brief read register
 *
 * @param[in] cdev pointer to console device
 * @param[in] offset
 *
 * @return value
 */
static uint32_t ns16550_read(struct console_device *cdev, uint32_t off)
{
	struct ns16550_priv *priv = to_ns16550_priv(cdev);
	struct device_d *dev = cdev->dev;
	struct NS16550_plat *plat = &priv->plat;
	int width = priv->access_width;

	off <<= plat->shift;

	switch (width) {
	case IORESOURCE_MEM_8BIT:
		return ns16550_sys_readb(dev->priv + off, priv->mmio);
	case IORESOURCE_MEM_16BIT:
		return ns16550_sys_readw(dev->priv + off, priv->mmio);
	case IORESOURCE_MEM_32BIT:
		return ns16550_sys_readl(dev->priv + off, priv->mmio);
	}
	return -1;
}

/**
 * @brief write register
 *
 * @param[in] cdev pointer to console device
 * @param[in] offset
 * @param[in] val
 */
static void ns16550_write(struct console_device *cdev, uint32_t val,
			  uint32_t off)
{
	struct ns16550_priv *priv = to_ns16550_priv(cdev);
	struct device_d *dev = cdev->dev;
	struct NS16550_plat *plat = &priv->plat;
	int width = priv->access_width;

	off <<= plat->shift;

	switch (width) {
	case IORESOURCE_MEM_8BIT:
		ns16550_sys_writeb(val & 0xff, dev->priv + off, priv->mmio);
		break;
	case IORESOURCE_MEM_16BIT:
		ns16550_sys_writew(val & 0xffff, dev->priv + off, priv->mmio);
		break;
	case IORESOURCE_MEM_32BIT:
		ns16550_sys_writel(val, dev->priv + off, priv->mmio);
		break;
	}
}

/**
 * @brief Compute the divisor for a baud rate
 *
 * @param[in] cdev pointer to console device
 * @param[in] baudrate baud rate
 *
 * @return divisor to be set
 */
static inline unsigned int ns16550_calc_divisor(struct console_device *cdev,
					 unsigned int baudrate)
{
	struct ns16550_priv *priv = to_ns16550_priv(cdev);
	struct NS16550_plat *plat = &priv->plat;
	unsigned int clk = plat->clock;

	return (clk / MODE_X_DIV / baudrate);

}

/**
 * @brief Set the baudrate for the uart port
 *
 * @param[in] cdev  console device
 * @param[in] baud_rate baud rate to set
 *
 * @return  0-implied to support the baudrate
 */
static int ns16550_setbaudrate(struct console_device *cdev, int baud_rate)
{
	unsigned int baud_divisor = ns16550_calc_divisor(cdev, baud_rate);
	struct ns16550_priv *priv = to_ns16550_priv(cdev);

	ns16550_write(cdev, LCR_BKSE, lcr);
	ns16550_write(cdev, baud_divisor & 0xff, dll);
	ns16550_write(cdev, (baud_divisor >> 8) & 0xff, dlm);
	ns16550_write(cdev, LCRVAL, lcr);
	ns16550_write(cdev, MCRVAL, mcr);
	ns16550_write(cdev, priv->fcrval, fcr);

	return 0;
}

/**
 * @brief Initialize the device
 *
 * @param[in] cdev pointer to console device
 */
static void ns16550_serial_init_port(struct console_device *cdev)
{
	/* initializing the device for the first time */
	ns16550_write(cdev, 0x00, lcr); /* select ier reg */
	ns16550_write(cdev, 0x00, ier);
}

static void ns16450_serial_init_port(struct console_device *cdev)
{
	struct ns16550_priv *priv = to_ns16550_priv(cdev);

	priv->fcrval &= ~FCR_FIFO_EN;

	ns16550_serial_init_port(cdev);
}

#define omap_mdr1		8

static void ns16550_omap_init_port(struct console_device *cdev)
{
	ns16550_serial_init_port(cdev);

	ns16550_write(cdev, 0x07, omap_mdr1);	/* Disable */
	ns16550_write(cdev, 0x00, omap_mdr1);
}

#define JZ_FCR_UME 0x10 /* Uart Module Enable */

static void ns16550_jz_init_port(struct console_device *cdev)
{
	struct ns16550_priv *priv = to_ns16550_priv(cdev);

	priv->fcrval |= JZ_FCR_UME;
	ns16550_serial_init_port(cdev);
}

/*********** Exposed Functions **********************************/

/**
 * @brief Put a character to the serial port
 *
 * @param[in] cdev pointer to console device
 * @param[in] c character to put
 */
static void ns16550_putc(struct console_device *cdev, char c)
{
	/* Loop Doing Nothing */
	while ((ns16550_read(cdev, lsr) & LSR_THRE) == 0) ;
	ns16550_write(cdev, c, thr);
}

/**
 * @brief Retrieve a character from serial port
 *
 * @param[in] cdev pointer to console device
 *
 * @return return the character read
 */
static int ns16550_getc(struct console_device *cdev)
{
	/* Loop Doing Nothing */
	while ((ns16550_read(cdev, lsr) & LSR_DR) == 0) ;
	return ns16550_read(cdev, rbr);
}

/**
 * @brief Test if character is available
 *
 * @param[in] cdev pointer to console device
 *
 * @return  - status based on data availability
 */
static int ns16550_tstc(struct console_device *cdev)
{
	return ((ns16550_read(cdev, lsr) & LSR_DR) != 0);
}

static void ns16550_probe_dt(struct device_d *dev, struct ns16550_priv *priv)
{
	struct device_node *np = dev->device_node;

	if (!IS_ENABLED(CONFIG_OFDEVICE))
		return;

	of_property_read_u32(np, "reg-shift", &priv->plat.shift);
}

static struct ns16550_drvdata ns16450_drvdata = {
	.init_port = ns16450_serial_init_port,
};

static struct ns16550_drvdata ns16550_drvdata = {
	.init_port = ns16550_serial_init_port,
};

static __maybe_unused struct ns16550_drvdata omap_drvdata = {
	.init_port = ns16550_omap_init_port,
	.linux_console_name = "ttyO",
};

static __maybe_unused struct ns16550_drvdata jz_drvdata = {
	.init_port = ns16550_jz_init_port,
};

/**
 * @brief Probe entry point -called on the first match for device
 *
 * @param[in] dev matched device
 *
 * @return EINVAL if platform_data is not populated,
 *	   ENOMEM if calloc failed
 *	   else return result of console_register
 */
static int ns16550_probe(struct device_d *dev)
{
	struct ns16550_priv *priv;
	struct console_device *cdev;
	struct NS16550_plat *plat = (struct NS16550_plat *)dev->platform_data;
	struct ns16550_drvdata *devtype;
	struct resource *res;
	int ret;

	ret = dev_get_drvdata(dev, (unsigned long *)&devtype);
	if (ret)
		devtype = &ns16550_drvdata;

	priv = xzalloc(sizeof(*priv));

	res = dev_get_resource(dev, IORESOURCE_MEM, 0);
	priv->mmio = (res != NULL);
	if (res) {
		res = request_iomem_region(dev_name(dev), res->start, res->end);
	} else {
		res = dev_get_resource(dev, IORESOURCE_IO, 0);
		if (res)
			res = request_ioport_region(dev_name(dev), res->start,
						    res->end);
	}
	if (!res)
		goto err;
	dev->priv = (void __force __iomem *) res->start;


	if (plat)
		priv->plat = *plat;
	else
		ns16550_probe_dt(dev, priv);

	if (!plat || !plat->clock) {
		priv->clk = clk_get(dev, NULL);
		if (IS_ERR(priv->clk)) {
			ret = PTR_ERR(priv->clk);
			goto err;
		}
		priv->plat.clock = clk_get_rate(priv->clk);
	}

	if (priv->plat.clock == 0 && IS_ENABLED(CONFIG_OFDEVICE)) {
		struct device_node *np = dev->device_node;

		of_property_read_u32(np, "clock-frequency", &priv->plat.clock);
	}

	if (priv->plat.clock == 0) {
		dev_err(dev, "no valid clockrate\n");
		ret = -EINVAL;
		goto err;
	}

	priv->access_width = dev->resource[0].flags & IORESOURCE_MEM_TYPE_MASK;

	cdev = &priv->cdev;
	cdev->dev = dev;
	cdev->tstc = ns16550_tstc;
	cdev->putc = ns16550_putc;
	cdev->getc = ns16550_getc;
	cdev->setbrg = ns16550_setbaudrate;
	cdev->linux_console_name = devtype->linux_console_name;

	if (plat && (plat->flags & NS16650_FLAG_DISABLE_FIFO))
		priv->fcrval = FCRVAL & ~FCR_FIFO_EN;
	else
		priv->fcrval = FCRVAL;

	devtype->init_port(cdev);

	return console_register(cdev);

err:
	free(priv);

	return ret;
}

static struct of_device_id ns16550_serial_dt_ids[] = {
	{
		.compatible = "ns16450",
		.data = (unsigned long)&ns16450_drvdata,
	}, {
		.compatible = "ns16550a",
		.data = (unsigned long)&ns16550_drvdata,
	}, {
		.compatible = "snps,dw-apb-uart",
		.data = (unsigned long)&ns16550_drvdata,
	},
#if IS_ENABLED(CONFIG_ARCH_OMAP)
	{
		.compatible = "ti,omap2-uart",
		.data = (unsigned long)&omap_drvdata,
	}, {
		.compatible = "ti,omap3-uart",
		.data = (unsigned long)&omap_drvdata,
	}, {
		.compatible = "ti,omap4-uart",
		.data = (unsigned long)&omap_drvdata,
	},
#endif
#if IS_ENABLED(CONFIG_MACH_MIPS_XBURST)
	{
		.compatible = "ingenic,jz4740-uart",
		.data = (unsigned long)&jz_drvdata,
	},
#endif
	{
		/* sentinel */
	},
};

static __maybe_unused struct platform_device_id ns16550_serial_ids[] = {
	{
		.name = "omap-uart",
		.driver_data = (unsigned long)&omap_drvdata,
	}, {
		/* sentinel */
	},
};

/**
 * @brief Driver registration structure
 */
static struct driver_d ns16550_serial_driver = {
	.name = "ns16550_serial",
	.probe = ns16550_probe,
	.of_compatible = DRV_OF_COMPAT(ns16550_serial_dt_ids),
#if IS_ENABLED(CONFIG_ARCH_OMAP)
	.id_table = ns16550_serial_ids,
#endif
};
console_platform_driver(ns16550_serial_driver);