summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/sh/shsim/gdbsci/gdbsci.c
blob: db6b4b1fa2ce09e48a8067246de51ccfdb68e9c5 (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
/*
 * /dev/gdbsci[0|1] for gdb's simulator's SH sci emulation
 *
 *  Author: Ralf Corsepius (corsepiu@faw.uni-ulm.de)
 *
 *  COPYRIGHT (c) 2001, Ralf Corsepius, Ulm, Germany
 *
 *  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.
 *
 *  $Id$
 */

#include <rtems.h>

#include <stdlib.h>

#include <rtems/libio.h>
#include <rtems/iosupp.h>
#include <rtems/score/sh_io.h>
/* HACK: There must be something better than this :) */
#if defined(__sh1__)
#include <rtems/score/ispsh7032.h>
#include <rtems/score/iosh7032.h>
#elif defined(__sh2__)
#include <rtems/score/ispsh7045.h>
#include <rtems/score/iosh7045.h>
#else
#error unsupported sh model
#endif
#include <sh/sh7_sci.h>
#include <sh/sh7_pfc.h>
#include <sh/sci.h>

/*
 * gdb assumes area 5/char access (base address & 0x0500000),
 * the RTEMS's sh7045 code however defaults to area 5/int/short/char access
 * [Very likely a bug in the sh7045 code, RC.]
 */

#define GDBSCI_BASE 0x05ffffff

#define GDBSCI0_SMR (SCI0_SMR & GDBSCI_BASE)
#define GDBSCI0_BRR (SCI0_BRR & GDBSCI_BASE)
#define GDBSCI0_SCR (SCI0_SCR & GDBSCI_BASE)
#define GDBSCI0_TDR (SCI0_TDR & GDBSCI_BASE)
#define GDBSCI0_SSR (SCI0_SSR & GDBSCI_BASE)
#define GDBSCI0_RDR (SCI0_RDR & GDBSCI_BASE)

#define GDBSCI1_SMR (SCI1_SMR & GDBSCI_BASE)
#define GDBSCI1_BRR (SCI1_BRR & GDBSCI_BASE)
#define GDBSCI1_SCR (SCI1_SCR & GDBSCI_BASE)
#define GDBSCI1_TDR (SCI1_TDR & GDBSCI_BASE)
#define GDBSCI1_SSR (SCI1_SSR & GDBSCI_BASE)
#define GDBSCI1_RDG (SCI1_RDR & GDBSCI_BASE)

/*
 * NOTE: Only device 1 is valid for the simulator
 */

#define SH_GDBSCI_MINOR_DEVICES       2

/* Force SIGBUS by using an unsupported address for /dev/gdbsci0 */
#define SH_GDBSCI_BASE_0   SCI0_SMR
#define SH_GDBSCI_BASE_1   GDBSCI1_SMR

struct scidev_t {
  char *			name ;
  uint32_t  			addr ;
  rtems_device_minor_number	minor ;
  unsigned short		opened ;
  tcflag_t			cflags ;
} sci_device[SH_GDBSCI_MINOR_DEVICES] =
{
  { "/dev/gdbsci0", SH_GDBSCI_BASE_0, 0, 0, B9600 | CS8 },
  { "/dev/gdbsci1", SH_GDBSCI_BASE_1, 1, 0, B9600 | CS8 }
} ;

/*  imported from scitab.rel */
extern int _sci_get_brparms(
  tcflag_t      cflag,
  unsigned char *smr,
  unsigned char *brr );

#if 0
/* Translate termios' tcflag_t into sci settings */
static int _sci_set_cflags(
  struct scidev_t      *sci_dev,
  tcflag_t      c_cflag )
{
  uint8_t  	smr;
  uint8_t  	brr;

  if ( c_cflag & CBAUD )
  {
    if ( _sci_get_brparms( c_cflag, &smr, &brr ) != 0 )
      return -1 ;
  }

  if ( c_cflag & CSIZE )
  {
    if ( c_cflag & CS8 )
      smr &= ~SCI_SEVEN_BIT_DATA;
    else if ( c_cflag & CS7 )
      smr |= SCI_SEVEN_BIT_DATA;
    else
      return -1 ;
  }

  if ( c_cflag & CSTOPB )
    smr |= SCI_STOP_BITS_2;
  else
    smr &= ~SCI_STOP_BITS_2;

  if ( c_cflag & PARENB )
    smr |= SCI_PARITY_ON ;
  else
    smr &= ~SCI_PARITY_ON ;

  if ( c_cflag & PARODD )
    smr |= SCI_ODD_PARITY ;
  else
    smr &= ~SCI_ODD_PARITY;

  write8( smr, sci_dev->addr + SCI_SMR );
  write8( brr, sci_dev->addr + SCI_BRR );

  return 0 ;
}
#endif

static void _sci_init(
  rtems_device_minor_number minor )
{
#if NOT_SUPPORTED_BY_GDB
  uint16_t  	temp16 ;

  /* Pin function controller initialisation for asynchronous mode */
  if( minor == 0)
    {
      temp16 = read16( PFC_PBCR1);
      temp16 &= ~( PB8MD | PB9MD );
      temp16 |= (PB_TXD0 | PB_RXD0);
      write16( temp16, PFC_PBCR1);
    }
  else
    {
      temp16 = read16( PFC_PBCR1);
      temp16 &= ~( PB10MD | PB11MD);
      temp16 |= (PB_TXD1 | PB_RXD1);
      write16( temp16, PFC_PBCR1);
    }

  /* disable sck-pin */
  if( minor == 0)
  {
	  temp16 = read16( PFC_PBCR1);
	  temp16 &= ~(PB12MD);
	  write16( temp16, PFC_PBCR1);
  }
  else
  {
	  temp16 = read16( PFC_PBCR1);
	  temp16 &= ~(PB13MD);
	  write16( temp16, PFC_PBCR1);
  }
#endif
}

static void _sci_tx_polled(
  int minor,
  const char buf )
{
  struct scidev_t *scidev = &sci_device[minor] ;
#if NOT_SUPPORTED_BY_GDB
  int8_t           ssr ;

  while ( !inb((scidev->addr + SCI_SSR) & SCI_TDRE ))
      ;
#endif
  write8(buf,scidev->addr+SCI_TDR);

#if NOT_SUPPORTED_BY_GDB
  ssr = inb(scidev->addr+SCI_SSR);
  ssr &= ~SCI_TDRE ;
  write8(ssr,scidev->addr+SCI_SSR);
#endif
}

static int _sci_rx_polled (
  int minor)
{
  struct scidev_t *scidev = &sci_device[minor] ;

  unsigned char c;
#if NOT_SUPPORTED_BY_GDB
  char ssr ;
  ssr = read8(scidev->addr + SCI_SSR) ;

  if (ssr & (SCI_PER | SCI_FER | SCI_ORER))
    write8(ssr & ~(SCI_PER | SCI_FER | SCI_ORER), scidev->addr+SCI_SSR);

  if ( !(ssr & SCI_RDRF) )
    return -1;
#endif
  c = read8(scidev->addr + SCI_RDR) ;
#if NOT_SUPPORTED_BY_GDB
  write8(ssr & ~SCI_RDRF,scidev->addr + SCI_SSR);
#endif
  return c;
}

/*
 * sci_initialize
 */

rtems_device_driver sh_gdbsci_initialize(
  rtems_device_major_number  major,
  rtems_device_minor_number  minor,
  void                      *arg )
{
  rtems_device_driver status ;
  rtems_device_minor_number     i;

  /*
   * register all possible devices.
   * the initialization of the hardware is done by sci_open
   */

  for ( i = 0 ; i < SH_GDBSCI_MINOR_DEVICES ; i++ )
  {
    status = rtems_io_register_name(
      sci_device[i].name,
      major,
      sci_device[i].minor );
    if (status != RTEMS_SUCCESSFUL)
      rtems_fatal_error_occurred(status);
  }

  /* default hardware setup */

  return RTEMS_SUCCESSFUL;
}


/*
 *  Open entry point
 */

rtems_device_driver sh_gdbsci_open(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void                    * arg )
{
#if NOT_SUPPORTED_BY_GDB
  uint8_t   temp8;
#endif
 /* check for valid minor number */
   if(( minor > ( SH_GDBSCI_MINOR_DEVICES -1 )) || ( minor < 0 ))
   {
     return RTEMS_INVALID_NUMBER;
   }

 /* device already opened */
  if ( sci_device[minor].opened > 0 )
  {
    sci_device[minor].opened++ ;
    return RTEMS_SUCCESSFUL ;
  }

  _sci_init( minor );

#if NOT_SUPPORTED_BY_GDB
  if (minor == 0) {
    temp8 = read8(sci_device[minor].addr + SCI_SCR);
    temp8 &= ~(SCI_TE | SCI_RE) ;
    write8(temp8, sci_device[minor].addr + SCI_SCR);	/* Clear SCR */
    _sci_set_cflags( &sci_device[minor], sci_device[minor].cflags );

/* FIXME: Should be one bit delay */
    CPU_delay(50000); /* microseconds */

    temp8 |= SCI_RE | SCI_TE;
    write8(temp8, sci_device[minor].addr + SCI_SCR);	/* Enable clock output */
  } else {
    temp8 = read8(sci_device[minor].addr + SCI_SCR);
    temp8 &= ~(SCI_TE | SCI_RE) ;
    write8(temp8, sci_device[minor].addr + SCI_SCR);	/* Clear SCR */
    _sci_set_cflags( &sci_device[minor], sci_device[minor].cflags );

/* FIXME: Should be one bit delay */
    CPU_delay(50000); /* microseconds */

    temp8 |= SCI_RE | SCI_TE;
    write8(temp8, sci_device[minor].addr + SCI_SCR);	/* Enable clock output */
  }
#endif

  sci_device[minor].opened++ ;

  return RTEMS_SUCCESSFUL ;
}

/*
 *  Close entry point
 */

rtems_device_driver sh_gdbsci_close(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void                    * arg
)
{
  if( sci_device[minor].opened == 0 )
    {
      return RTEMS_INVALID_NUMBER;
    }

  sci_device[minor].opened-- ;

  return RTEMS_SUCCESSFUL ;
}

/*
 * read bytes from the serial port.
 */

rtems_device_driver sh_gdbsci_read(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void                    * arg
)
{
  int count = 0;

  rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *) arg;
  char * buffer = rw_args->buffer;
  int maximum = rw_args->count;

  for (count = 0; count < maximum; count++) {
    buffer[ count ] = _sci_rx_polled(minor);
    if (buffer[ count ] == '\n' || buffer[ count ] == '\r') {
      buffer[ count++ ]  = '\n';
      break;
    }
  }

  rw_args->bytes_moved = count;
  return (count >= 0) ? RTEMS_SUCCESSFUL : RTEMS_UNSATISFIED;
}

/*
 * write bytes to the serial port.
 */

rtems_device_driver sh_gdbsci_write(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void                    * arg
)
{
  int count;

  rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *) arg;
  char *buffer = rw_args->buffer;
  int maximum = rw_args->count;

  for (count = 0; count < maximum; count++) {
    _sci_tx_polled( minor, buffer[ count ] );
  }

  rw_args->bytes_moved = maximum;
  return 0;
}

/*
 *  IO Control entry point
 */

rtems_device_driver sh_gdbsci_control(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void                    * arg
)
{
  /* Not yet supported */
  return RTEMS_SUCCESSFUL ;
}