sync hh.org
[hh.org.git] / arch / ppc / boot / simple / iic.c
blobe4efd838bfaad51a05ae10b8945270bc1edf3279
1 /* Minimal support functions to read configuration from IIC EEPROMS
2 * on MPC8xx boards. Originally written for RPGC RPX-Lite.
3 * Dan Malek (dmalek@jlc.net).
4 */
5 #include <linux/types.h>
6 #include <asm/uaccess.h>
7 #include <asm/mpc8xx.h>
8 #include <asm/commproc.h>
11 /* IIC functions.
12 * These are just the basic master read/write operations so we can
13 * examine serial EEPROM.
15 void iic_read(uint devaddr, u_char *buf, uint offset, uint count);
17 static int iic_init_done;
19 static void
20 iic_init(void)
22 volatile iic_t *iip;
23 volatile i2c8xx_t *i2c;
24 volatile cpm8xx_t *cp;
25 volatile immap_t *immap;
26 uint dpaddr;
28 immap = (immap_t *)IMAP_ADDR;
29 cp = (cpm8xx_t *)&(immap->im_cpm);
31 /* Reset the CPM. This is necessary on the 860 processors
32 * that may have started the SCC1 ethernet without relocating
33 * the IIC.
34 * This also stops the Ethernet in case we were loaded by a
35 * BOOTP rom monitor.
37 cp->cp_cpcr = (CPM_CR_RST | CPM_CR_FLG);
39 /* Wait for it.
41 while (cp->cp_cpcr & (CPM_CR_RST | CPM_CR_FLG));
43 /* Remove any microcode patches. We will install our own
44 * later.
46 cp->cp_cpmcr1 = 0;
47 cp->cp_cpmcr2 = 0;
48 cp->cp_cpmcr3 = 0;
49 cp->cp_cpmcr4 = 0;
50 cp->cp_rccr = 0;
52 iip = (iic_t *)&cp->cp_dparam[PROFF_IIC];
53 i2c = (i2c8xx_t *)&(immap->im_i2c);
55 /* Initialize Port B IIC pins.
57 cp->cp_pbpar |= 0x00000030;
58 cp->cp_pbdir |= 0x00000030;
59 cp->cp_pbodr |= 0x00000030;
61 /* Initialize the parameter ram.
64 /* Allocate space for a two transmit and one receive buffer
65 * descriptor in the DP ram.
66 * For now, this address seems OK, but it may have to
67 * change with newer versions of the firmware.
69 dpaddr = 0x0840;
71 /* Set up the IIC parameters in the parameter ram.
73 iip->iic_tbase = dpaddr;
74 iip->iic_rbase = dpaddr + (2 * sizeof(cbd_t));
76 iip->iic_tfcr = SMC_EB;
77 iip->iic_rfcr = SMC_EB;
79 /* This should really be done by the reader/writer.
81 iip->iic_mrblr = 128;
83 /* Initialize Tx/Rx parameters.
85 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_I2C, CPM_CR_INIT_TRX) | CPM_CR_FLG;
86 while (cp->cp_cpcr & CPM_CR_FLG);
88 /* Select an arbitrary address. Just make sure it is unique.
90 i2c->i2c_i2add = 0x34;
92 /* Make clock run maximum slow.
94 i2c->i2c_i2brg = 7;
96 /* Disable interrupts.
98 i2c->i2c_i2cmr = 0;
99 i2c->i2c_i2cer = 0xff;
101 /* Enable SDMA.
103 immap->im_siu_conf.sc_sdcr = 1;
105 iic_init_done = 1;
108 /* Read from IIC.
109 * Caller provides device address, memory buffer, and byte count.
111 static u_char iitemp[32];
113 void
114 iic_read(uint devaddr, u_char *buf, uint offset, uint count)
116 volatile iic_t *iip;
117 volatile i2c8xx_t *i2c;
118 volatile cbd_t *tbdf, *rbdf;
119 volatile cpm8xx_t *cp;
120 volatile immap_t *immap;
121 u_char *tb;
122 uint temp;
124 /* If the interface has not been initialized, do that now.
126 if (!iic_init_done)
127 iic_init();
129 immap = (immap_t *)IMAP_ADDR;
130 cp = (cpm8xx_t *)&(immap->im_cpm);
132 iip = (iic_t *)&cp->cp_dparam[PROFF_IIC];
133 i2c = (i2c8xx_t *)&(immap->im_i2c);
135 tbdf = (cbd_t *)&cp->cp_dpmem[iip->iic_tbase];
136 rbdf = (cbd_t *)&cp->cp_dpmem[iip->iic_rbase];
138 /* Send a "dummy write" operation. This is a write request with
139 * only the offset sent, followed by another start condition.
140 * This will ensure we start reading from the first location
141 * of the EEPROM.
143 tb = iitemp;
144 tb = (u_char *)(((uint)tb + 15) & ~15);
145 tbdf->cbd_bufaddr = (int)tb;
146 *tb = devaddr & 0xfe; /* Device address */
147 *(tb+1) = offset; /* Offset */
148 tbdf->cbd_datlen = 2; /* Length */
149 tbdf->cbd_sc =
150 BD_SC_READY | BD_SC_LAST | BD_SC_WRAP | BD_IIC_START;
152 i2c->i2c_i2mod = 1; /* Enable */
153 i2c->i2c_i2cer = 0xff;
154 i2c->i2c_i2com = 0x81; /* Start master */
156 /* Wait for IIC transfer.
158 #if 0
159 while ((i2c->i2c_i2cer & 3) == 0);
161 if (tbdf->cbd_sc & BD_SC_READY)
162 printf("IIC ra complete but tbuf ready\n");
163 #else
164 temp = 10000000;
165 while ((tbdf->cbd_sc & BD_SC_READY) && (temp != 0))
166 temp--;
167 #if 0
168 /* We can't do this...there is no serial port yet!
170 if (temp == 0) {
171 printf("Timeout reading EEPROM\n");
172 return;
174 #endif
175 #endif
177 /* Chip errata, clear enable.
179 i2c->i2c_i2mod = 0;
181 /* To read, we need an empty buffer of the proper length.
182 * All that is used is the first byte for address, the remainder
183 * is just used for timing (and doesn't really have to exist).
185 tbdf->cbd_bufaddr = (int)tb;
186 *tb = devaddr | 1; /* Device address */
187 rbdf->cbd_bufaddr = (uint)buf; /* Desination buffer */
188 tbdf->cbd_datlen = rbdf->cbd_datlen = count + 1; /* Length */
189 tbdf->cbd_sc = BD_SC_READY | BD_SC_LAST | BD_SC_WRAP | BD_IIC_START;
190 rbdf->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
192 /* Chip bug, set enable here.
194 i2c->i2c_i2mod = 1; /* Enable */
195 i2c->i2c_i2cer = 0xff;
196 i2c->i2c_i2com = 0x81; /* Start master */
198 /* Wait for IIC transfer.
200 #if 0
201 while ((i2c->i2c_i2cer & 1) == 0);
203 if (rbdf->cbd_sc & BD_SC_EMPTY)
204 printf("IIC read complete but rbuf empty\n");
205 #else
206 temp = 10000000;
207 while ((tbdf->cbd_sc & BD_SC_READY) && (temp != 0))
208 temp--;
209 #endif
211 /* Chip errata, clear enable.
213 i2c->i2c_i2mod = 0;