summaryrefslogtreecommitdiff
path: root/sys/arch/mips/alchemy/dev/ausmbus_psc.c
blob: 725bd8d023837ca78ed0fb8b4e1153d2e8cd3422 (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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/* $NetBSD: ausmbus_psc.c,v 1.16 2022/06/18 22:11:00 andvar Exp $ */

/*-
 * Copyright (c) 2006 Shigeyuki Fukushima.
 * All rights reserved.
 *
 * Written by Shigeyuki Fukushima.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ausmbus_psc.c,v 1.16 2022/06/18 22:11:00 andvar Exp $");

#include "locators.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/errno.h>

#include <sys/bus.h>
#include <machine/cpu.h>

#include <mips/alchemy/dev/aupscreg.h>
#include <mips/alchemy/dev/aupscvar.h>
#include <mips/alchemy/dev/ausmbus_pscreg.h>

#include <dev/i2c/i2cvar.h>
#include <dev/i2c/i2c_bitbang.h>

struct ausmbus_softc {
	device_t			sc_dev;

	/* protocol comoon fields */
	struct aupsc_controller		sc_ctrl;

	/* protocol specific fields */
	struct i2c_controller		sc_i2c;
	i2c_addr_t			sc_smbus_slave_addr;
	int				sc_smbus_timeout;
};

#define	ausmbus_reg_read(sc, reg) \
	bus_space_read_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush, reg)
#define	ausmbus_reg_write(sc, reg, val) \
	bus_space_write_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush, reg, \
		val); \
	delay(100);

static int	ausmbus_match(device_t, struct cfdata *, void *);
static void	ausmbus_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(ausmbus, sizeof(struct ausmbus_softc),
	ausmbus_match, ausmbus_attach, NULL, NULL);

/* functions for i2c_controller */
static int	ausmbus_acquire_bus(void *, int);
static void	ausmbus_release_bus(void *, int);
static int	ausmbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
				const void *cmd, size_t cmdlen, void *vbuf,
				size_t buflen, int flags);

/* subroutine functions for i2c_controller */
static int	ausmbus_quick_write(struct ausmbus_softc *);
static int	ausmbus_quick_read(struct ausmbus_softc *);
static int	ausmbus_receive_1(struct ausmbus_softc *, uint8_t *);
static int	ausmbus_read_1(struct ausmbus_softc *, uint8_t, uint8_t *);
static int	ausmbus_read_2(struct ausmbus_softc *, uint8_t, uint16_t *);
static int	ausmbus_send_1(struct ausmbus_softc *, uint8_t);
static int	ausmbus_write_1(struct ausmbus_softc *, uint8_t, uint8_t);
static int	ausmbus_write_2(struct ausmbus_softc *, uint8_t, uint16_t);
static int	ausmbus_wait_mastertx(struct ausmbus_softc *sc);
static int	ausmbus_wait_masterrx(struct ausmbus_softc *sc);
static int	ausmbus_initiate_xfer(void *, i2c_addr_t, int);
static int	ausmbus_read_byte(void *arg, uint8_t *vp, int flags);
static int	ausmbus_write_byte(void *arg, uint8_t v, int flags);


static int
ausmbus_match(device_t parent, struct cfdata *cf, void *aux)
{
	struct aupsc_attach_args *aa = (struct aupsc_attach_args *)aux;

	if (strcmp(aa->aupsc_name, cf->cf_name) != 0)
		return 0;

	return 1;
}

static void
ausmbus_attach(device_t parent, device_t self, void *aux)
{
	struct ausmbus_softc *sc = device_private(self);
	struct aupsc_attach_args *aa = (struct aupsc_attach_args *)aux;
	struct i2cbus_attach_args iba;

	aprint_normal(": Alchemy PSC SMBus protocol\n");

	sc->sc_dev = self;

	/* Initialize PSC */
	sc->sc_ctrl = aa->aupsc_ctrl;

	/* Initialize i2c_controller for SMBus */
	iic_tag_init(&sc->sc_i2c);
	sc->sc_i2c.ic_cookie = sc;
	sc->sc_i2c.ic_acquire_bus = ausmbus_acquire_bus;
	sc->sc_i2c.ic_release_bus = ausmbus_release_bus;
	sc->sc_i2c.ic_exec = ausmbus_exec;
	sc->sc_smbus_timeout = 10;

	memset(&iba, 0, sizeof(iba));
	iba.iba_tag = &sc->sc_i2c;
	config_found(self, &iba, iicbus_print, CFARGS_NONE);
}

static int
ausmbus_acquire_bus(void *arg, int flags)
{
	struct ausmbus_softc *sc = arg;
	uint32_t v;

	/* Select SMBus Protocol & Enable PSC */
	sc->sc_ctrl.psc_enable(sc, AUPSC_SEL_SMBUS);
	v = ausmbus_reg_read(sc, AUPSC_SMBSTAT);
	if ((v & SMBUS_STAT_SR) == 0) {
		/* PSC is not ready */
		return -1;
	}

	/* Setup SMBus Configuration register */
	v = SMBUS_CFG_DD;				/* Disable DMA */
	v |= SMBUS_CFG_RT_SET(SMBUS_CFG_RT_FIFO8);	/* Rx FIFO 8data */
	v |= SMBUS_CFG_TT_SET(SMBUS_CFG_TT_FIFO8);	/* Tx FIFO 8data */
	v |= SMBUS_CFG_DIV_SET(SMBUS_CFG_DIV8);		/* pscn_mainclk/8 */
	v &= ~SMBUS_CFG_SFM;				/* Standard Mode */
	ausmbus_reg_write(sc, AUPSC_SMBCFG, v);

	/* Setup SMBus Protocol Timing register */
	v = SMBUS_TMR_TH_SET(SMBUS_TMR_STD_TH)
		| SMBUS_TMR_PS_SET(SMBUS_TMR_STD_PS)
		| SMBUS_TMR_PU_SET(SMBUS_TMR_STD_PU)
		| SMBUS_TMR_SH_SET(SMBUS_TMR_STD_SH)
		| SMBUS_TMR_SU_SET(SMBUS_TMR_STD_SU)
		| SMBUS_TMR_CL_SET(SMBUS_TMR_STD_CL)
		| SMBUS_TMR_CH_SET(SMBUS_TMR_STD_CH);
	ausmbus_reg_write(sc, AUPSC_SMBTMR, v);

	/* Setup SMBus Mask register */
	v = SMBUS_MSK_ALLMASK;
	ausmbus_reg_write(sc, AUPSC_SMBMSK, v);

	/* SMBus Enable */
	v = ausmbus_reg_read(sc, AUPSC_SMBCFG);
	v |= SMBUS_CFG_DE;
	ausmbus_reg_write(sc, AUPSC_SMBCFG, v);
	v = ausmbus_reg_read(sc, AUPSC_SMBSTAT);
	if ((v & SMBUS_STAT_SR) == 0) {
		/* SMBus is not ready */
		return -1;
	}

#ifdef AUSMBUS_PSC_DEBUG
	aprint_normal("AuSMBus enabled.\n");
	aprint_normal("AuSMBus smbconfig: 0x%08x\n",
			ausmbus_reg_read(sc, AUPSC_SMBCFG));
	aprint_normal("AuSMBus smbstatus: 0x%08x\n",
			ausmbus_reg_read(sc, AUPSC_SMBSTAT));
	aprint_normal("AuSMBus smbtmr   : 0x%08x\n",
			ausmbus_reg_read(sc, AUPSC_SMBTMR));
	aprint_normal("AuSMBus smbmask  : 0x%08x\n",
			ausmbus_reg_read(sc, AUPSC_SMBMSK));
#endif

	return 0;
}

static void
ausmbus_release_bus(void *arg, int flags)
{
	struct ausmbus_softc *sc = arg;

	ausmbus_reg_write(sc, AUPSC_SMBCFG, 0);
	sc->sc_ctrl.psc_disable(sc);

	return;
}

static int
ausmbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *vcmd,
	size_t cmdlen, void *vbuf, size_t buflen, int flags)
{
	struct ausmbus_softc *sc  = (struct ausmbus_softc *)cookie;
	const uint8_t *cmd = vcmd;

	sc->sc_smbus_slave_addr  = addr;

	/* Receive byte */
	if ((I2C_OP_READ_P(op)) && (cmdlen == 0) && (buflen == 1)) {
		return ausmbus_receive_1(sc, (uint8_t *)vbuf);
	}

	/* Read byte */
	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
		return ausmbus_read_1(sc, *cmd, (uint8_t *)vbuf);
	}

	/* Read word */
	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) {
		return ausmbus_read_2(sc, *cmd, (uint16_t *)vbuf);
	}

	/* Read quick */
	if ((I2C_OP_READ_P(op)) && (cmdlen == 0) && (buflen == 0)) {
		return ausmbus_quick_read(sc);
	}

	/* Send byte */
	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1)) {
		return ausmbus_send_1(sc, *((uint8_t *)vbuf));
	}

	/* Write byte */
	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1)) {
		return ausmbus_write_1(sc, *cmd, *((uint8_t *)vbuf));
	}

	/* Write word */
	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2)) {
		return ausmbus_write_2(sc, *cmd, *((uint16_t *)vbuf));
	}

	/* Write quick */
	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 0)) {
		return ausmbus_quick_write(sc);
	}

	/*
	 * XXX: TODO Please Support other protocols defined in SMBus 2.0
	 * - Process call
	 * - Block write/read
	 * - Clock write-block read process cal
	 * - SMBus host notify protocol
	 *
	 * - Read quick and write quick have not been tested!
	 */

	return -1;
}

static int
ausmbus_receive_1(struct ausmbus_softc *sc, uint8_t *vp)
{
	int error;

	error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_READ);
	if (error != 0) {
		return error;
	}
	error = ausmbus_read_byte(sc, vp, I2C_F_STOP);
	if (error != 0) {
		return error;
	}

	return 0;
}

static int
ausmbus_read_1(struct ausmbus_softc *sc, uint8_t cmd, uint8_t *vp)
{
	int error;

	error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_WRITE);
	if (error != 0) {
		return error;
	}

	error = ausmbus_write_byte(sc, cmd, I2C_F_READ);
	if (error != 0) {
		return error;
	}

	error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_READ);
	if (error != 0) {
		return error;
	}

	error = ausmbus_read_byte(sc, vp, I2C_F_STOP);
	if (error != 0) {
		return error;
	}

	return 0;
}

static int
ausmbus_read_2(struct ausmbus_softc *sc, uint8_t cmd, uint16_t *vp)
{
	int error;
	uint8_t high, low;

	error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_WRITE);
	if (error != 0) {
		return error;
	}

	error = ausmbus_write_byte(sc, cmd, I2C_F_READ);
	if (error != 0) {
		return error;
	}

	error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_READ);
	if (error != 0) {
		return error;
	}

	error = ausmbus_read_byte(sc, &low, 0);
	if (error != 0) {
		return error;
	}

	error = ausmbus_read_byte(sc, &high, I2C_F_STOP);
	if (error != 0) {
		return error;
	}

	*vp = (high << 8) | low;

	return 0;
}

static int
ausmbus_send_1(struct ausmbus_softc *sc, uint8_t val)
{
	int error;

	error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_WRITE);
	if (error != 0) {
		return error;
	}

	error = ausmbus_write_byte(sc, val, I2C_F_STOP);
	if (error != 0) {
		return error;
	}

	return 0;
}

static int
ausmbus_write_1(struct ausmbus_softc *sc, uint8_t cmd, uint8_t val)
{
	int error;

	error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_WRITE);
	if (error != 0) {
		return error;
	}

	error = ausmbus_write_byte(sc, cmd, 0);
	if (error != 0) {
		return error;
	}

	error = ausmbus_write_byte(sc, val, I2C_F_STOP);
	if (error != 0) {
		return error;
	}

	return 0;
}

static int
ausmbus_write_2(struct ausmbus_softc *sc, uint8_t cmd, uint16_t val)
{
	int error;
	uint8_t high, low;

	high = (val >> 8) & 0xff;
	low = val & 0xff;

	error = ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr, I2C_F_WRITE);
	if (error != 0) {
		return error;
	}

	error = ausmbus_write_byte(sc, cmd, 0);
	if (error != 0) {
		return error;
	}

	error = ausmbus_write_byte(sc, low, 0);
	if (error != 0) {
		return error;
	}

	error = ausmbus_write_byte(sc, high, I2C_F_STOP);
	if (error != 0) {
		return error;
	}

	return 0;
}

/*
 * XXX The quick_write() and quick_read() routines have not been tested!
 */
static int
ausmbus_quick_write(struct ausmbus_softc *sc)
{
	return ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr,
			I2C_F_STOP | I2C_F_WRITE);
}

static int
ausmbus_quick_read(struct ausmbus_softc *sc)
{
	return ausmbus_initiate_xfer(sc, sc->sc_smbus_slave_addr,
			I2C_F_STOP | I2C_F_READ);
}

static int
ausmbus_wait_mastertx(struct ausmbus_softc *sc)
{
	uint32_t v;
	int timeout;
	int txerr = 0;

	timeout = sc->sc_smbus_timeout;

	do {
		v = ausmbus_reg_read(sc, AUPSC_SMBEVNT);
#ifdef AUSMBUS_PSC_DEBUG
		aprint_normal("AuSMBus: ausmbus_wait_mastertx(): psc_smbevnt=0x%08x\n", v);
#endif
		if ((v & SMBUS_EVNT_TU) != 0)
			break;
		if ((v & SMBUS_EVNT_MD) != 0)
			break;
		if ((v & (SMBUS_EVNT_DN | SMBUS_EVNT_AN | SMBUS_EVNT_AL))
			!= 0) {
			txerr = 1;
			break;
		}
		timeout--;
		delay(1);
	} while (timeout > 0);

	if (txerr != 0) {
		ausmbus_reg_write(sc, AUPSC_SMBEVNT,
			SMBUS_EVNT_DN | SMBUS_EVNT_AN | SMBUS_EVNT_AL);
#ifdef AUSMBUS_PSC_DEBUG
		aprint_normal("AuSMBus: ausmbus_wait_mastertx(): Tx error\n");
#endif
		return -1;
	}

	/* Reset Event TU (Tx Underflow) */
	ausmbus_reg_write(sc, AUPSC_SMBEVNT, SMBUS_EVNT_TU | SMBUS_EVNT_MD);

#ifdef AUSMBUS_PSC_DEBUG
	v = ausmbus_reg_read(sc, AUPSC_SMBEVNT);
	aprint_normal("AuSMBus: ausmbus_wait_mastertx(): psc_smbevnt=0x%08x (reset)\n", v);
#endif
	return 0;
}

static int
ausmbus_wait_masterrx(struct ausmbus_softc *sc)
{
	uint32_t v;
	int timeout;
	timeout = sc->sc_smbus_timeout;

	if (ausmbus_wait_mastertx(sc) != 0)
		return -1;

	do {
		v = ausmbus_reg_read(sc, AUPSC_SMBSTAT);
#ifdef AUSMBUS_PSC_DEBUG
		aprint_normal("AuSMBus: ausmbus_wait_masterrx(): psc_smbstat=0x%08x\n", v);
#endif
		if ((v & SMBUS_STAT_RE) == 0)
			break;
		timeout--;
		delay(1);
	} while (timeout > 0);

	return 0;
}

static int
ausmbus_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
{
	struct ausmbus_softc *sc = arg;
	uint32_t v;

	/* Tx/Rx Slave Address */
	v = (addr << 1) & SMBUS_TXRX_ADDRDATA;
	if ((flags & I2C_F_READ) != 0)
		v |= 1;
	if ((flags & I2C_F_STOP) != 0)
		v |= SMBUS_TXRX_STP;
	ausmbus_reg_write(sc, AUPSC_SMBTXRX, v);

	/* Master Start */
	ausmbus_reg_write(sc, AUPSC_SMBPCR, SMBUS_PCR_MS);

	if (ausmbus_wait_mastertx(sc) != 0)
		return -1;

	return 0;
}

static int
ausmbus_read_byte(void *arg, uint8_t *vp, int flags)
{
	struct ausmbus_softc *sc = arg;
	uint32_t v;

	if ((flags & I2C_F_STOP) != 0) {
		ausmbus_reg_write(sc, AUPSC_SMBTXRX, SMBUS_TXRX_STP);
	} else {
		ausmbus_reg_write(sc, AUPSC_SMBTXRX, 0);
	}

	if (ausmbus_wait_masterrx(sc) != 0)
		return -1;

	v = ausmbus_reg_read(sc, AUPSC_SMBTXRX);
	*vp = v & SMBUS_TXRX_ADDRDATA;

	return 0;
}

static int
ausmbus_write_byte(void *arg, uint8_t v, int flags)
{
	struct ausmbus_softc *sc = arg;

	if ((flags & I2C_F_STOP) != 0)  {
		ausmbus_reg_write(sc, AUPSC_SMBTXRX, (v | SMBUS_TXRX_STP));
	} else if ((flags & I2C_F_READ) != 0) {
		ausmbus_reg_write(sc, AUPSC_SMBTXRX, (v | SMBUS_TXRX_RSR));
	} else {
		ausmbus_reg_write(sc, AUPSC_SMBTXRX, v);
	}

	if (ausmbus_wait_mastertx(sc) != 0)
		return -1;

	return 0;
}