1#ifndef __SERIAL_H__
2#define __SERIAL_H__
3
4#include <post.h>
5
6struct serial_device {
7	/* enough bytes to match alignment of following func pointer */
8	char	name[16];
9
10	int	(*start)(void);
11	int	(*stop)(void);
12	void	(*setbrg)(void);
13	int	(*getc)(void);
14	int	(*tstc)(void);
15	void	(*putc)(const char c);
16	void	(*puts)(const char *s);
17#if CFG_POST & CFG_SYS_POST_UART
18	void	(*loop)(int);
19#endif
20	struct serial_device	*next;
21};
22
23void default_serial_puts(const char *s);
24
25extern struct serial_device serial_smc_device;
26extern struct serial_device serial_smh_device;
27extern struct serial_device serial_scc_device;
28extern struct serial_device *default_serial_console(void);
29
30#if	defined(CONFIG_MPC83xx) || defined(CONFIG_MPC85xx) || \
31	defined(CONFIG_MPC86xx) || \
32	defined(CONFIG_ARCH_TEGRA) || defined(CONFIG_SYS_COREBOOT) || \
33	defined(CONFIG_MICROBLAZE)
34extern struct serial_device serial0_device;
35extern struct serial_device serial1_device;
36#endif
37
38extern struct serial_device eserial1_device;
39extern struct serial_device eserial2_device;
40extern struct serial_device eserial3_device;
41extern struct serial_device eserial4_device;
42extern struct serial_device eserial5_device;
43extern struct serial_device eserial6_device;
44
45extern void serial_register(struct serial_device *);
46extern void serial_stdio_init(void);
47extern int serial_assign(const char *name);
48extern void serial_reinit_all(void);
49int serial_initialize(void);
50
51/* For usbtty */
52#ifdef CONFIG_USB_TTY
53
54struct stdio_dev;
55
56int usbtty_getc(struct stdio_dev *dev);
57void usbtty_putc(struct stdio_dev *dev, const char c);
58void usbtty_puts(struct stdio_dev *dev, const char *str);
59int usbtty_tstc(struct stdio_dev *dev);
60
61#else
62
63/* stubs */
64#define usbtty_getc(dev) 0
65#define usbtty_putc(dev, a)
66#define usbtty_puts(dev, a)
67#define usbtty_tstc(dev) 0
68
69#endif /* CONFIG_USB_TTY */
70
71struct udevice;
72
73enum serial_par {
74	SERIAL_PAR_NONE,
75	SERIAL_PAR_ODD,
76	SERIAL_PAR_EVEN
77};
78
79#define SERIAL_PAR_SHIFT	0
80#define SERIAL_PAR_MASK		(0x03 << SERIAL_PAR_SHIFT)
81#define SERIAL_SET_PARITY(parity) \
82	((parity << SERIAL_PAR_SHIFT) & SERIAL_PAR_MASK)
83#define SERIAL_GET_PARITY(config) \
84	((config & SERIAL_PAR_MASK) >> SERIAL_PAR_SHIFT)
85
86enum serial_bits {
87	SERIAL_5_BITS,
88	SERIAL_6_BITS,
89	SERIAL_7_BITS,
90	SERIAL_8_BITS
91};
92
93#define SERIAL_BITS_SHIFT	2
94#define SERIAL_BITS_MASK	(0x3 << SERIAL_BITS_SHIFT)
95#define SERIAL_SET_BITS(bits) \
96	((bits << SERIAL_BITS_SHIFT) & SERIAL_BITS_MASK)
97#define SERIAL_GET_BITS(config) \
98	((config & SERIAL_BITS_MASK) >> SERIAL_BITS_SHIFT)
99
100enum serial_stop {
101	SERIAL_HALF_STOP,	/* 0.5 stop bit */
102	SERIAL_ONE_STOP,	/*   1 stop bit */
103	SERIAL_ONE_HALF_STOP,	/* 1.5 stop bit */
104	SERIAL_TWO_STOP		/*   2 stop bit */
105};
106
107#define SERIAL_STOP_SHIFT	4
108#define SERIAL_STOP_MASK	(0x3 << SERIAL_STOP_SHIFT)
109#define SERIAL_SET_STOP(stop) \
110	((stop << SERIAL_STOP_SHIFT) & SERIAL_STOP_MASK)
111#define SERIAL_GET_STOP(config) \
112	((config & SERIAL_STOP_MASK) >> SERIAL_STOP_SHIFT)
113
114#define SERIAL_CONFIG(par, bits, stop) \
115		     (par << SERIAL_PAR_SHIFT | \
116		      bits << SERIAL_BITS_SHIFT | \
117		      stop << SERIAL_STOP_SHIFT)
118
119#define SERIAL_DEFAULT_CONFIG \
120			(SERIAL_PAR_NONE << SERIAL_PAR_SHIFT | \
121			 SERIAL_8_BITS << SERIAL_BITS_SHIFT | \
122			 SERIAL_ONE_STOP << SERIAL_STOP_SHIFT)
123
124enum serial_chip_type {
125	SERIAL_CHIP_UNKNOWN = -1,
126	SERIAL_CHIP_16550_COMPATIBLE,
127};
128
129enum adr_space_type {
130	SERIAL_ADDRESS_SPACE_MEMORY = 0,
131	SERIAL_ADDRESS_SPACE_IO,
132};
133
134/**
135 * struct serial_device_info - structure to hold serial device info
136 *
137 * @type:	type of the UART chip
138 * @addr_space:	address space to access the registers
139 * @addr:	physical address of the registers
140 * @size:	size of the register area in bytes
141 * @reg_width:	size (in bytes) of the IO accesses to the registers
142 * @reg_offset:	offset to apply to the @addr from the start of the registers
143 * @reg_shift:	quantity to shift the register offsets by
144 * @clock:	UART base clock speed in Hz
145 * @baudrate:	baud rate
146 */
147struct serial_device_info {
148	enum serial_chip_type type;
149	enum adr_space_type addr_space;
150	ulong addr;
151	ulong size;
152	u8 reg_width;
153	u8 reg_offset;
154	u8 reg_shift;
155	unsigned int clock;
156	unsigned int baudrate;
157};
158
159#define SERIAL_DEFAULT_ADDRESS	0xBADACCE5
160#define SERIAL_DEFAULT_CLOCK	(16 * 115200)
161
162/**
163 * struct struct dm_serial_ops - Driver model serial operations
164 *
165 * The uclass interface is implemented by all serial devices which use
166 * driver model.
167 */
168struct dm_serial_ops {
169	/**
170	 * setbrg() - Set up the baud rate generator
171	 *
172	 * Adjust baud rate divisors to set up a new baud rate for this
173	 * device. Not all devices will support all rates. If the rate
174	 * cannot be supported, the driver is free to select the nearest
175	 * available rate. or return -EINVAL if this is not possible.
176	 *
177	 * @dev: Device pointer
178	 * @baudrate: New baud rate to use
179	 * @return 0 if OK, -ve on error
180	 */
181	int (*setbrg)(struct udevice *dev, int baudrate);
182	/**
183	 * getc() - Read a character and return it
184	 *
185	 * If no character is available, this should return -EAGAIN without
186	 * waiting.
187	 *
188	 * @dev: Device pointer
189	 * @return character (0..255), -ve on error
190	 */
191	int (*getc)(struct udevice *dev);
192	/**
193	 * putc() - Write a character
194	 *
195	 * @dev: Device pointer
196	 * @ch: character to write
197	 * @return 0 if OK, -ve on error
198	 */
199	int (*putc)(struct udevice *dev, const char ch);
200	/**
201	 * puts() - Write a string
202	 *
203	 * This writes a string. This function should be implemented only if
204	 * writing multiple characters at once is more performant than just
205	 * calling putc() in a loop.
206	 *
207	 * If the whole string cannot be written at once, then this function
208	 * should return the number of characters written. Returning a negative
209	 * error code implies that no characters were written. If this function
210	 * returns 0, then it will be called again with the same arguments.
211	 *
212	 * @dev: Device pointer
213	 * @s: The string to write
214	 * @len: The length of the string to write.
215	 * @return The number of characters written on success, or -ve on error
216	 */
217	ssize_t (*puts)(struct udevice *dev, const char *s, size_t len);
218	/**
219	 * pending() - Check if input/output characters are waiting
220	 *
221	 * This can be used to return an indication of the number of waiting
222	 * characters if the driver knows this (e.g. by looking at the FIFO
223	 * level). It is acceptable to return 1 if an indeterminant number
224	 * of characters is waiting.
225	 *
226	 * This method is optional.
227	 *
228	 * @dev: Device pointer
229	 * @input: true to check input characters, false for output
230	 * @return number of waiting characters, 0 for none, -ve on error
231	 */
232	int (*pending)(struct udevice *dev, bool input);
233	/**
234	 * clear() - Clear the serial FIFOs/holding registers
235	 *
236	 * This method is optional.
237	 *
238	 * This quickly clears any input/output characters from the UART.
239	 * If this is not possible, but characters still exist, then it
240	 * is acceptable to return -EAGAIN (try again) or -EINVAL (not
241	 * supported).
242	 *
243	 * @dev: Device pointer
244	 * @return 0 if OK, -ve on error
245	 */
246	int (*clear)(struct udevice *dev);
247#if CFG_POST & CFG_SYS_POST_UART
248	/**
249	 * loop() - Control serial device loopback mode
250	 *
251	 * @dev: Device pointer
252	 * @on: 1 to turn loopback on, 0 to turn if off
253	 */
254	int (*loop)(struct udevice *dev, int on);
255#endif
256
257	/**
258	 * getconfig() - Get the uart configuration
259	 * (parity, 5/6/7/8 bits word length, stop bits)
260	 *
261	 * Get a current config for this device.
262	 *
263	 * @dev: Device pointer
264	 * @serial_config: Returns config information (see SERIAL_... above)
265	 * @return 0 if OK, -ve on error
266	 */
267	int (*getconfig)(struct udevice *dev, uint *serial_config);
268	/**
269	 * setconfig() - Set up the uart configuration
270	 * (parity, 5/6/7/8 bits word length, stop bits)
271	 *
272	 * Set up a new config for this device.
273	 *
274	 * @dev: Device pointer
275	 * @serial_config: number of bits, parity and number of stopbits to use
276	 * @return 0 if OK, -ve on error
277	 */
278	int (*setconfig)(struct udevice *dev, uint serial_config);
279	/**
280	 * getinfo() - Get serial device information
281	 *
282	 * @dev: Device pointer
283	 * @info: struct serial_device_info to fill
284	 * @return 0 if OK, -ve on error
285	 */
286	int (*getinfo)(struct udevice *dev, struct serial_device_info *info);
287};
288
289/**
290 * struct serial_dev_priv - information about a device used by the uclass
291 *
292 * @sdev:	stdio device attached to this uart
293 *
294 * @buf:	Pointer to the RX buffer
295 * @rd_ptr:	Read pointer in the RX buffer
296 * @wr_ptr:	Write pointer in the RX buffer
297 */
298struct serial_dev_priv {
299	struct stdio_dev *sdev;
300
301	char *buf;
302	int rd_ptr;
303	int wr_ptr;
304};
305
306/* Access the serial operations for a device */
307#define serial_get_ops(dev)	((struct dm_serial_ops *)(dev)->driver->ops)
308
309/**
310 * serial_getconfig() - Get the uart configuration
311 * (parity, 5/6/7/8 bits word length, stop bits)
312 *
313 * Get a current config for this device.
314 *
315 * @dev: Device pointer
316 * @serial_config: Returns config information (see SERIAL_... above)
317 * Return: 0 if OK, -ve on error
318 */
319int serial_getconfig(struct udevice *dev, uint *config);
320
321/**
322 * serial_setconfig() - Set up the uart configuration
323 * (parity, 5/6/7/8 bits word length, stop bits)
324 *
325 * Set up a new config for this device.
326 *
327 * @dev: Device pointer
328 * @serial_config: number of bits, parity and number of stopbits to use
329 * Return: 0 if OK, -ve on error
330 */
331int serial_setconfig(struct udevice *dev, uint config);
332
333/**
334 * serial_getinfo() - Get serial device information
335 *
336 * @dev: Device pointer
337 * @info: struct serial_device_info to fill
338 * Return: 0 if OK, -ve on error
339 */
340int serial_getinfo(struct udevice *dev, struct serial_device_info *info);
341
342/**
343 * fetch_baud_from_dtb() - Fetch the baudrate value from DT
344 *
345 * Return: baudrate if OK, -ve on error
346 */
347int fetch_baud_from_dtb(void);
348
349void atmel_serial_initialize(void);
350void mcf_serial_initialize(void);
351void mpc85xx_serial_initialize(void);
352void mxc_serial_initialize(void);
353void ns16550_serial_initialize(void);
354void pl01x_serial_initialize(void);
355void pxa_serial_initialize(void);
356void sh_serial_initialize(void);
357
358/**
359 * serial_printf() - Write a formatted string to the serial console
360 *
361 * The total size of the output must be less than CONFIG_SYS_PBSIZE.
362 *
363 * @fmt: Printf format string, followed by format arguments
364 * Return: number of characters written
365 */
366int serial_printf(const char *fmt, ...)
367		__attribute__ ((format (__printf__, 1, 2)));
368
369int serial_init(void);
370void serial_setbrg(void);
371void serial_putc(const char ch);
372void serial_putc_raw(const char ch);
373void serial_puts(const char *str);
374#if defined(CONFIG_CONSOLE_FLUSH_SUPPORT) && CONFIG_IS_ENABLED(DM_SERIAL)
375void serial_flush(void);
376#else
377static inline void serial_flush(void) {}
378#endif
379int serial_getc(void);
380int serial_tstc(void);
381
382#endif
383