1/*
2 * Misc utility routines for accessing PMU corerev specific features
3 * of the SiliconBackplane-based Broadcom chips.
4 *
5 * Copyright (C) 1999-2010, Broadcom Corporation
6 *
7 *      Unless you and Broadcom execute a separate written software license
8 * agreement governing use of this software, this software is licensed to you
9 * under the terms of the GNU General Public License version 2 (the "GPL"),
10 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
11 * following added to such license:
12 *
13 *      As a special exception, the copyright holders of this software give you
14 * permission to link this software with independent modules, and to copy and
15 * distribute the resulting executable under terms of your choice, provided that
16 * you also meet, for each linked independent module, the terms and conditions of
17 * the license of that module.  An independent module is a module which is not
18 * derived from this software.  The special exception does not apply to any
19 * modifications of the software.
20 *
21 *      Notwithstanding the above, under no circumstances may you combine this
22 * software in any way with any other Broadcom software provided under a license
23 * other than the GPL, without Broadcom's express prior written consent.
24 *
25 * $Id: hndpmu.c,v 1.95.2.17.4.11.2.63 2010/07/21 13:55:09 Exp $
26 */
27
28#include <typedefs.h>
29#include <bcmdefs.h>
30#include <osl.h>
31#include <bcmutils.h>
32#include <siutils.h>
33#include <bcmdevs.h>
34#include <hndsoc.h>
35#include <sbchipc.h>
36#include <hndpmu.h>
37
38/* debug/trace */
39#define	PMU_ERROR(args)
40
41#define	PMU_MSG(args)
42
43
44/* SDIO Pad drive strength to select value mappings */
45typedef struct {
46	uint8 strength;			/* Pad Drive Strength in mA */
47	uint8 sel;			/* Chip-specific select value */
48} sdiod_drive_str_t;
49
50/* SDIO Drive Strength to sel value table for PMU Rev 1 */
51static const sdiod_drive_str_t sdiod_drive_strength_tab1[] = {
52	{4, 0x2},
53	{2, 0x3},
54	{1, 0x0},
55	{0, 0x0} };
56
57/* SDIO Drive Strength to sel value table for PMU Rev 2, 3 */
58static const sdiod_drive_str_t sdiod_drive_strength_tab2[] = {
59	{12, 0x7},
60	{10, 0x6},
61	{8, 0x5},
62	{6, 0x4},
63	{4, 0x2},
64	{2, 0x1},
65	{0, 0x0} };
66
67#define SDIOD_DRVSTR_KEY(chip, pmu)	(((chip) << 16) | (pmu))
68
69void
70si_sdiod_drive_strength_init(si_t *sih, osl_t *osh, uint32 drivestrength)
71{
72	chipcregs_t *cc;
73	uint origidx, intr_val = 0;
74	sdiod_drive_str_t *str_tab = NULL;
75	uint32 str_mask = 0;
76	uint32 str_shift = 0;
77
78	if (!(sih->cccaps & CC_CAP_PMU)) {
79		return;
80	}
81
82	/* Remember original core before switch to chipc */
83	cc = (chipcregs_t *) si_switch_core(sih, CC_CORE_ID, &origidx, &intr_val);
84
85	switch (SDIOD_DRVSTR_KEY(sih->chip, sih->pmurev)) {
86	case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 1):
87		str_tab = (sdiod_drive_str_t *)&sdiod_drive_strength_tab1;
88		str_mask = 0x30000000;
89		str_shift = 28;
90		break;
91	case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 2):
92	case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 3):
93	case SDIOD_DRVSTR_KEY(BCM4315_CHIP_ID, 4):
94		str_tab = (sdiod_drive_str_t *)&sdiod_drive_strength_tab2;
95		str_mask = 0x00003800;
96		str_shift = 11;
97		break;
98
99	default:
100		PMU_MSG(("No SDIO Drive strength init done for chip %x rev %d pmurev %d\n",
101		         sih->chip, sih->chiprev, sih->pmurev));
102
103		break;
104	}
105
106	if (str_tab != NULL) {
107		uint32 drivestrength_sel = 0;
108		uint32 cc_data_temp;
109		int i;
110
111		for (i = 0; str_tab[i].strength != 0; i ++) {
112			if (drivestrength >= str_tab[i].strength) {
113				drivestrength_sel = str_tab[i].sel;
114				break;
115			}
116		}
117
118		W_REG(osh, &cc->chipcontrol_addr, 1);
119		cc_data_temp = R_REG(osh, &cc->chipcontrol_data);
120		cc_data_temp &= ~str_mask;
121		drivestrength_sel <<= str_shift;
122		cc_data_temp |= drivestrength_sel;
123		W_REG(osh, &cc->chipcontrol_data, cc_data_temp);
124
125		PMU_MSG(("SDIO: %dmA drive strength selected, set to 0x%08x\n",
126		         drivestrength, cc_data_temp));
127	}
128
129	/* Return to original core */
130	si_restore_core(sih, origidx, intr_val);
131}
132