summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/powerpc/ppcn_60x/console/config.c
blob: 4c7c4879cfc1752860d6ec701a9b160b98731e6f (plain) (blame)
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
/*
 *  This file contains the TTY driver table for the PPCn_60x
 *
 *  COPYRIGHT (c) 1998 by Radstone Technology
 *
 *
 * THIS FILE IS PROVIDED TO YOU, THE USER, "AS IS", WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK
 * AS TO THE QUALITY AND PERFORMANCE OF ALL CODE IN THIS FILE IS WITH YOU.
 *
 * You are hereby granted permission to use, copy, modify, and distribute
 * this file, provided that this notice, plus the above copyright notice
 * and disclaimer, appears in all copies. Radstone Technology will provide
 * no support for this code.
 *
 *  $Id$
 */

#include <libchip/serial.h>
#include <libchip/mc68681.h>
#include <libchip/z85c30.h>
#include "i8042vga.h"

#include "ns16550cfg.h"
#include "z85c30cfg.h"

#include <pci.h>

#define PMX1553_BUS	2
#define PMX1553_SLOT	1

/*
 *  Based on BSP configuration information decide whether to do polling IO
 *  or interrupt driven IO.
 */

#if (CONSOLE_USE_INTERRUPTS)
#define NS16550_FUNCTIONS &ns16550_fns
#define Z85C30_FUNCTIONS  &z85c30_fns
#else
#define NS16550_FUNCTIONS &ns16550_fns_polled
#define Z85C30_FUNCTIONS  &z85c30_fns_polled
#endif

/*
 * Configuration specific probe routines
 */
static boolean config_PMX1553_probe(int minor);
static boolean config_z85c30_probe(int minor);

/*
 * The following table configures the console drivers used in this BSP.
 *
 * The first entry which, when probed, is available, will be named /dev/console,
 * all others being given the name indicated.
 *
 * Each field is interpreted thus:
 *
 * sDeviceName	This is the name of the device.
 * pDeviceFns	This is a pointer to the set of driver routines to use.
 * pDeviceFlow	This is a pointer to the set of flow control routines to
 *		use. Serial device drivers will typically supply RTSCTS
 *		and DTRCTS handshake routines for DCE to DCE communication,
 *		however for DCE to DTE communication, no such routines
 *		should be necessary as RTS will be driven automatically
 *		when the transmitter is active.
 * ulMargin	The high water mark in the input buffer is set to the buffer
 *		size less ulMargin. Once this level is reached, the driver's
 *		flow control routine used to stop the remote transmitter will
 *		be called. This figure should be greater than or equal to
 *		the number of stages of FIFO between the transmitter and
 *		receiver.
 * ulHysteresis	After the high water mark specified by ulMargin has been
 *		reached, the driver's routine to re-start the remote
 *		transmitter will be called once the level in the input
 *		buffer has fallen by ulHysteresis bytes.
 * pDeviceParams This contains either device specific data or a pointer to a
 *		device specific structure containing additional information
 *		not provided in this table.
 * ulCtrlPort1	This is the primary control port number for the device. This
 *		may be used to specify different instances of the same device
 *		type.
 * ulCtrlPort2	This is the secondary control port number, of use when a given
 *		device has more than one available channel.
 * ulDataPort	This is the port number for the data port of the device
 * ulIntVector	This encodes the interrupt vector of the device.
 *
 */
console_tbl	Console_Port_Tbl[] = {
	{
		"/dev/vga",			/* sDeviceName */
		SERIAL_CUSTOM,			/* deviceType */
		&i8042vga_fns,			/* pDeviceFns */
		NULL,				/* deviceProbe */
		NULL,				/* pDeviceFlow */
		0,				/* ulMargin */
		0,				/* ulHysteresis */
		(void *)0,			/* pDeviceParams */
		I8042_CS,			/* ulCtrlPort1 */
		0,				/* ulCtrlPort2 */
		I8042_DATA,			/* ulDataPort */
		Read_ns16550_register,		/* getRegister */
		Write_ns16550_register,		/* setRegister */
		NULL,				/* getData */
		NULL,				/* setData */
		0,				/* ulClock */
		PPCN_60X_IRQ_KBD		/* ulIntVector */
	},
	{
		"/dev/com1",			/* sDeviceName */
		SERIAL_NS16550,			/* deviceType */
		NS16550_FUNCTIONS,		/* pDeviceFns */
		NULL,				/* deviceProbe */
		&ns16550_flow_RTSCTS,		/* pDeviceFlow */
		16,				/* ulMargin */
		8,				/* ulHysteresis */
		(void *)9600,	/* baud rate */	/* pDeviceParams */
		NS16550_PORT_A,			/* ulCtrlPort1 */
		0,				/* ulCtrlPort2 */
		NS16550_PORT_A,			/* ulDataPort */
		Read_ns16550_register,		/* getRegister */
		Write_ns16550_register,		/* setRegister */
		NULL,				/* getData */
		NULL,				/* setData */
		0,				/* ulClock */
		PPCN_60X_IRQ_COM1		/* ulIntVector */
	},
	{
		"/dev/ser1",			/* sDeviceName */
		SERIAL_NS16550,			/* deviceType */
		NS16550_FUNCTIONS,		/* pDeviceFns */
		config_PMX1553_probe,		/* deviceProbe */
		&ns16550_flow_RTSCTS,		/* pDeviceFlow */
		80,				/* ulMargin */
		8,				/* ulHysteresis */
		(void *)9600,	/* baud rate */	/* pDeviceParams */
		PMX1553_BUS,	/* PCI bus */	/* ulCtrlPort1 */
		PMX1553_SLOT,	/* PCI slot */	/* ulCtrlPort2 */
		1,	/* Channel 1-4 */	/* ulDataPort */
		NULL,				/* getRegister */
		NULL,				/* setRegister */
		NULL,				/* getData */
		NULL,				/* setData */
		0,				/* ulClock */
		0	/* RS232 */		/* ulIntVector */
	},
	{
		"/dev/ser2",			/* sDeviceName */
		SERIAL_NS16550,			/* deviceType */
		NS16550_FUNCTIONS,		/* pDeviceFns */
		config_PMX1553_probe,		/* deviceProbe */
		&ns16550_flow_RTSCTS,		/* pDeviceFlow */
		80,				/* ulMargin */
		8,				/* ulHysteresis */
		(void *)9600,	/* baud rate */	/* pDeviceParams */
		PMX1553_BUS,	/* PCI bus */	/* ulCtrlPort1 */
		PMX1553_SLOT,	/* PCI slot */	/* ulCtrlPort2 */
		2,	/* Channel 1-4 */	/* ulDataPort */
		Read_ns16550_register,		/* getRegister */
		Write_ns16550_register,		/* setRegister */
		NULL,				/* getData */
		NULL,				/* setData */
		0,				/* ulClock */
		0	/* RS232 */		/* ulIntVector */
	},
	{
		"/dev/ser3",			/* sDeviceName */
		SERIAL_NS16550,			/* deviceType */
		NS16550_FUNCTIONS,		/* pDeviceFns */
		config_PMX1553_probe,		/* deviceProbe */
		&ns16550_flow_RTSCTS,		/* pDeviceFlow */
		96,				/* ulMargin */
		8,				/* ulHysteresis */
		(void *)57600,	/* baud rate */	/* pDeviceParams */
		PMX1553_BUS,	/* PCI bus */	/* ulCtrlPort1 */
		PMX1553_SLOT,	/* PCI slot */	/* ulCtrlPort2 */
		3,	/* Channel 1-4 */	/* ulDataPort */
		Read_ns16550_register,		/* getRegister */
		Write_ns16550_register,		/* setRegister */
		NULL,				/* getData */
		NULL,				/* setData */
		0,				/* ulClock */
		0	/* RS232 */		/* ulIntVector */
	},
	{
		"/dev/ser4",			/* sDeviceName */
		SERIAL_NS16550,			/* deviceType */
		NS16550_FUNCTIONS,		/* pDeviceFns */
		config_PMX1553_probe,		/* deviceProbe */
		&ns16550_flow_RTSCTS,		/* pDeviceFlow */
		96,				/* ulMargin */
		8,				/* ulHysteresis */
		(void *)57600,	/* baud rate */	/* pDeviceParams */
		PMX1553_BUS,	/* PCI bus */	/* ulCtrlPort1 */
		PMX1553_SLOT,	/* PCI slot */	/* ulCtrlPort2 */
		4,	/* Channel 1-4 */	/* ulDataPort */
		Read_ns16550_register,		/* getRegister */
		Write_ns16550_register,		/* setRegister */
		NULL,				/* getData */
		NULL,				/* setData */
		0,				/* ulClock */
		0	/* RS232 */		/* ulIntVector */
	},
#if !PPCN_60X_USE_DINK
	{
		"/dev/com2",			/* sDeviceName */
		SERIAL_NS16550,			/* deviceType */
		NS16550_FUNCTIONS,		/* pDeviceFns */
		NULL,				/* deviceProbe */
		&ns16550_flow_RTSCTS,		/* pDeviceFlow */
		16,				/* ulMargin */
		8,				/* ulHysteresis */
		(void *)9600,	/* baud rate */	/* pDeviceParams */
		NS16550_PORT_B,			/* ulCtrlPort1 */
		0,				/* ulCtrlPort2 */
		NS16550_PORT_B,			/* ulDataPort */
		Read_ns16550_register,		/* getRegister */
		Write_ns16550_register,		/* setRegister */
		NULL,				/* getData */
		NULL,				/* setData */
		0,				/* ulClock */
		PPCN_60X_IRQ_COM2		/* ulIntVector */
	},
#endif
	{
		"/dev/com3",			/* sDeviceName */
		SERIAL_Z85C30,			/* deviceType */
		Z85C30_FUNCTIONS,		/* pDeviceFns */
		config_z85c30_probe,		/* deviceProbe */
		&z85c30_flow_RTSCTS,		/* pDeviceFlow */
		16,				/* ulMargin */
		8,				/* ulHysteresis */
		(void *)9600,	/* baud rate */	/* pDeviceParams */
		Z85C30_CTRL_A,			/* ulCtrlPort1 */
		Z85C30_CTRL_A,			/* ulCtrlPort2 */
		Z85C30_DATA_A,			/* ulDataPort */
		Read_85c30_register,		/* getRegister */
		Write_85c30_register,		/* setRegister */
		Read_85c30_data,		/* getData */
		Write_85c30_data,		/* setData */
		0,				/* ulClock */
		PPCN_60X_IRQ_COM3_4		/* ulIntVector */
	},
	{
		"/dev/com4",			/* sDeviceName */
		SERIAL_Z85C30,			/* deviceType */
		Z85C30_FUNCTIONS,		/* pDeviceFns */
		config_z85c30_probe,		/* deviceProbe */
		&z85c30_flow_RTSCTS,		/* pDeviceFlow */
		16,				/* ulMargin */
		8,				/* ulHysteresis */
		(void *)9600,	/* baud rate */	/* pDeviceParams */
		Z85C30_CTRL_B,			/* ulCtrlPort1 */
		Z85C30_CTRL_A,			/* ulCtrlPort2 */
		Z85C30_DATA_B,			/* ulDataPort */
		Read_85c30_register,		/* getRegister */
		Write_85c30_register,		/* setRegister */
		Read_85c30_data,		/* getData */
		Write_85c30_data,		/* setData */
		0,				/* ulClock */
		PPCN_60X_IRQ_COM3_4		/* ulIntVector */
	}
};

/*
 * Define serial port write registers structure.
 */
typedef volatile struct _SP_WRITE_REGISTERS {
    unsigned char TransmitBuffer;
    unsigned char InterruptEnable;
    unsigned char FifoControl;
    unsigned char LineControl;
    unsigned char ModemControl;
    unsigned char Reserved1;
    unsigned char ModemStatus;
    unsigned char ScratchPad;
} SP_WRITE_REGISTERS, *PSP_WRITE_REGISTERS;

static boolean config_PMX1553_probe(int minor)
{
	uint8_t   ucBusNumber, ucSlotNumber, ucChannel;
	uint8_t   ucIntLine;
	uint32_t   ulPortBase, ulMemBase, ulDeviceID;
	uint8_t   *pucSIO_cir, *pucUart_int_sr, *pucUartDevIntReg;
	PSP_WRITE_REGISTERS     pNS16550Write;

	/*
	 * Extract PCI bus/slot and channel number
	 */
	ucBusNumber=Console_Port_Tbl[minor].ulCtrlPort1;
	ucSlotNumber=Console_Port_Tbl[minor].ulCtrlPort2;
	ucChannel=Console_Port_Tbl[minor].ulDataPort;

	PCIConfigRead32(ucBusNumber,
			ucSlotNumber,
			0,
			PCI_CONFIG_VENDOR_LOW,
			&ulDeviceID);

	if(ulDeviceID!=0x000111b5)
	{
		return FALSE;
	}

	/*
	 * At this point we know we have a PMC1553 or PMX1553 card
	 *
	 * Check for PMX1553 uart legacy IO ports
	 */
	PCIConfigRead32(ucBusNumber,
			ucSlotNumber,
			0,
			PCI_CONFIG_BAR_3,
			&ulPortBase);

	if(ulPortBase==0)
	{
		/*
		 * This is either a PMC1553 or we can't see the uart
		 * registers
		 */
		return FALSE;
	}

	PCIConfigRead32(ucBusNumber,
			ucSlotNumber,
			0,
			PCI_CONFIG_BAR_2,
			&ulMemBase);

	pucUartDevIntReg=(uint8_t*)(PCI_MEM_BASE+ulMemBase);
	pucUart_int_sr=(uint8_t*)(PCI_MEM_BASE+ulMemBase+0x10);
	pucSIO_cir=(uint8_t*)(PCI_MEM_BASE+ulMemBase+0x18);

	/*
	 * Use ulIntVector field to select RS232/RS422
	 */
	if(Console_Port_Tbl[minor].ulIntVector==0)
	{
		/*
		 * Select RS232 mode
		 */
		*pucSIO_cir&=~(1<<(ucChannel-1));
	}
	else
	{
		/*
		 * Select RS422 mode
		 */
		*pucSIO_cir|=1<<(ucChannel-1);
	}
	EIEIO;
	/*
	 * Bring device out of reset
	 */
	*pucSIO_cir&=0xbf;
	EIEIO;
	/*
	 * Enable all channels as active
	 */
	*pucSIO_cir|=0x10;
	EIEIO;
	*pucSIO_cir&=0xdf;

	PCIConfigRead8(ucBusNumber,
		       ucSlotNumber,
		       0,
		       PCI_CONFIG_INTERRUPTLINE,
		       &ucIntLine);

	ulPortBase&=~PCI_ADDRESS_IO_SPACE;

	ulPortBase+=8*(ucChannel-1);

	Console_Port_Tbl[minor].ulCtrlPort1=
	Console_Port_Tbl[minor].ulDataPort=ulPortBase;
	if(Console_Port_Tbl[minor].pDeviceFns!=&ns16550_fns_polled)
	{
		Console_Port_Tbl[minor].ulIntVector=PPCN_60X_IRQ_PCI(ucIntLine);

		/*
		 * Enable interrupt
		 */
		*pucUart_int_sr=(~*pucUart_int_sr)&(0x08<<ucChannel);

		/*
		 * Enable interrupt to PCI
		 */
		*pucUartDevIntReg=(~*pucUartDevIntReg)&0x80;
	}
	else
	{
		/*
		 * Disable interrupt
		 */
		*pucUart_int_sr&=(0x08<<ucChannel);
	}

	/*
	 * Enable Auto CTS to facilitate flow control
	 */
	pNS16550Write=(PSP_WRITE_REGISTERS)Console_Port_Tbl[minor].ulCtrlPort1;
	/*
	 * Enable special register set and unlock Enhanced Feature Register
	 */
	outport_byte(&pNS16550Write->LineControl, 0xbf);
	/*
	 * Unlock enhanced function bits
	 */
	outport_byte(&pNS16550Write->FifoControl, 0x10);
	/*
	 * Disable special register set and lock Enhanced Feature Register
	 */
	outport_byte(&pNS16550Write->LineControl, 0);
	/*
	 * Select div 1
	 */
	outport_byte(&pNS16550Write->ModemControl, 0x00);
	/*
	 * Enable special register set and unlock Enhanced Feature Register
	 */
	outport_byte(&pNS16550Write->LineControl, 0xbf);
	/*
	 * Lock enhanced function bits and enable auto CTS
	 */
	outport_byte(&pNS16550Write->FifoControl, 0x80);
	/*
	 * Disable special register set and lock Enhanced Feature Register
	 */
	outport_byte(&pNS16550Write->LineControl, 0);
	
	/*
	 * The PMX1553 currently uses a 16 MHz clock rather than the
	 * 7.3728 MHz clock described in the ST16C654 data sheet. When
	 * available, 22.1184 MHz will be used allowing rates up to
	 * 1382400 baud (RS422 only).
	 */
#if 1
	/*
	 * Scale requested baud rate for 16 MHz clock
	 */
	(uint32_t)Console_Port_Tbl[minor].pDeviceParams*=7373;
	(uint32_t)Console_Port_Tbl[minor].pDeviceParams/=16000;
#else
	/*
	 * Scale requested baud rate for 22.1184 MHz clock
	 */
	(uint32_t)Console_Port_Tbl[minor].pDeviceParams/=3;
#endif
	/*
	 * In order to maintain maximum data rate accuracy, we will
	 * apply a div 4 here rather than in hardware (using MCR bit 7).
	 */
	(uint32_t)Console_Port_Tbl[minor].pDeviceParams/=4;

	return(TRUE);
}

static boolean config_z85c30_probe(int minor)
{
	/*
	 * PPC1 and PPC1a do not have this device
	 */
	if((ucSystemType==SYS_TYPE_PPC1) ||
	   (ucSystemType==SYS_TYPE_PPC1a))
	{
		return (FALSE);
	}

	/*
	 * All other boards supported by this BSP have the z85c30 device
	 */

	/*
	 * Ensure that CIO port B is configured for
	 * default driver enable
	 */
	outport_byte(0x861, 0x33);

	return(TRUE);
}